有没有一种方法可以使用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
这个链接中的代码:http://social.msdn.microsoft.com/Forums/pl/exceldev/thread/90384567-3338-4c56-bc6b-70db94d8cbcc
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);
}
当 while 循环结束时,rowIdx 是第一个空行的索引。
CountA 接收其他 29 个可选参数,所以在早期版本的框架中可能需要传递 Missing.Value 29 次。
有一个Range.End属性或Range.get_End方法,结合方向,可以给出范围内第一个空单元格。
可以查看这个问题的可能解决方案:如何使用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
但是,如果你有一列数据,你知道每个单元格都会填满,直到最后一个单元格,那么你可以向下获取结尾。