简体   繁体   English

使用ExcelDNA或NetOffice插入Excel行/列

[英]Insert Excel Rows/Columns with ExcelDNA or NetOffice

I am using ExcelDNA to set and get cell values. 我正在使用ExcelDNA设置和获取单元格值。 I can get and set cell values 我可以获取并设置单元格值

var ref = ExcelReference(2, 2);
var val = ref.GetValue();
ref.SetValue(42);

is there a way to insert an entire row or column by moving entries to the right or down? 有没有一种方法可以通过将条目向右或向下移动来插入整个行或列? I want the same behavior as when the user right clicks the column and all the entries are shifted to the right. 我想要与用户右键单击该列并将所有条目都移到右侧时相同的行为。 Solution can use NetOffice if necessary. 解决方案可以根据需要使用NetOffice。

I'd recommend using the COM object model for this, and the code will be similar to VBA for the same task. 我建议为此使用COM对象模型,并且同一任务的代码将类似于VBA。

You get hold of the root Application object with a call to ExcelDnaUtil.Application . 您可以通过调用ExcelDnaUtil.ApplicationExcelDnaUtil.ApplicationApplication对象。 The resulting object will be of type Microsoft.Office.Interop.Excel.Application and can be used to select the row of column, then call app.Selection.Insert() to insert the new row or column. 生成的对象将是Microsoft.Office.Interop.Excel.Application类型,可用于选择列的行,然后调用app.Selection.Insert()以插入新的行或列。

It should also be possible using the C API, but that is unlikely to be easier or faster. 使用C API也应该有可能,但这不太可能变得更容易或更快速。

I would like to add that NetOffice does not support the EntireRow and EntireColumn methods of the Range object, which would be useful for inserting or deleting full rows. 我想补充一点,NetOffice不支持Range对象的EntireRowEntireColumn方法,这对于插入或删除完整行很有用。 As a workaround, one can replace this for rows by addressing full rows by Range(rowNoStart + ":" + rowNoEnd) . 作为一种解决方法,可以通过用Range(rowNoStart + ":" + rowNoEnd)寻址完整的行来代替行。

For columns, one can write Range(GetExcelColumnName(colStart) + ":" + GetExcelColumnName(colEnd)) , where GetExcelColumnName is a function from this former SO post . 对于列,可以编写Range(GetExcelColumnName(colStart) + ":" + GetExcelColumnName(colEnd)) ,其中GetExcelColumnName一篇SO文章中的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM