Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# excel中的第一个空行_C#_Excel_Vsto - Fatal编程技术网

C# excel中的第一个空行

C# excel中的第一个空行,c#,excel,vsto,C#,Excel,Vsto,是否有方法使用vsto或c#查找excel工作表中的第一个可用空行? 我已经找了几个小时了,但找不到任何与之相关的东西。 在这方面的任何帮助都将不胜感激 Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)excelbk.Worksheets["Sheet1"]; xlRange = (Excel.Range)xlWorkSheet1.Cells[xlWorkSheet1.Rows.Count, 1]; long lastRow = (long)xlR

是否有方法使用vsto或c#查找excel工作表中的第一个可用空行? 我已经找了几个小时了,但找不到任何与之相关的东西。 在这方面的任何帮助都将不胜感激

Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];
xlRange = (Excel.Range)xlWorkSheet1.Cells[xlWorkSheet1.Rows.Count, 1];
long lastRow = (long)xlRange.get_End(Excel.XlDirection.xlUp).Row;
Long newRow = lastRow + 1

此链接中的代码:

有一个Range.End属性或Range.get\u End方法,它与一个方向相结合,将为您提供范围中的第一个空白单元格

看看这个问题,寻找可能的解决方案:
关键在于:

Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)excelbk.Worksheets["Sheet1"];
xlRange = (Excel.Range)xlWorkSheet1.Cells[xlWorkSheet1.Rows.Count, 1];
long lastRow = (long)xlRange.get_End(Excel.XlDirection.xlUp).Row;
Long newRow = lastRow + 1

但是,如果您知道某个列在最后一个单元格之前都会填充每个单元格,那么您就可以向下找到该列的结尾。

如果应用程序是您的excel应用程序,您可以循环遍历从数字1开始的行,并使用CountA函数查找第一个空单元格(它返回某个范围内非空单元格的数量):

当while循环退出时,rowIdx是第一个空行的索引


CountA会收到其他29个可选参数,因此您可能必须在早期版本的框架中传递Missing.Value 29次。

它显示最后使用的行。但是我想找到第一个空白行,因为最后使用的可以是任何地方,所以你真的想要第一个空白行,即使它后面的行不是空的?
int rowIdx = 0;
int notEmpty = 1;
while (notEmpty > 0)
{
    string aCellAddress = "A" + (++rowIdx).ToString();
    Range row = App.get_Range(aCellAddress, aCellAddress).EntireRow;
    notEmpty = _App.WorksheetFunction.CountA(row);
}