简体   繁体   English

excel中的第一个空白行

[英]first blank row in excel

Is there a way to find first available blank row in excel sheet using vsto or c# ? 有没有办法使用vsto或c#在excel表中找到第一个可用的空白行? I have been searching for hours but cant find any thing related to it. 我一直在寻找几个小时,但无法找到任何相关的东西。 Any help in this regard will be greatly appreciated 在这方面的任何帮助将不胜感激

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

Code from this link: http://social.msdn.microsoft.com/Forums/pl/exceldev/thread/90384567-3338-4c56-bc6b-70db94d8cbcc 此链接中的代码: http//social.msdn.microsoft.com/Forums/pl/exceldev/thread/90384567-3338-4c56-bc6b-70db94d8cbcc

There's a Range.End property or Range.get_End method which combined with a direction will give you the first blank cell in a range. 有一个Range.End属性或Range.get_End方法与方向结合将为您提供范围中的第一个空白单元格。

Have a look at this question for a possible solution: How to find the Next blank row in Excel sheet programatically using C# 查看此问题以获得可能的解决方案: 如何使用C#以编程方式查找Excel工作表中的下一个空白行
The key bit being: 关键是:

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

But if you've got a column that you know will always have every cell filled until the last one then you could get the end in the down direction. 但是如果你有一个你知道的专栏将会一直填充到最后一个单元格,那么你就可以在向下的方向上结束。

If App is your excel application, you can loop throught the rows starting from number 1 and look for the first empty using CountA function (it returns the number of non empty cells for a Range): 如果App是你的excel应用程序,你可以循环遍历从1开始的行,并使用CountA函数查找第一个空(它返回一个Range的非空单元格数):

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);
}

When the while loop exits, rowIdx is the index of the first empty row. 当while循环退出时,rowIdx是第一个空行的索引。

CountA receive other 29 optional arguments, so it is possible that you have to pass Missing.Value 29 times with earlier versions of the framework. CountA接收其他29个可选参数,因此您可能必须使用早期版本的框架将Missing.Value传递29次。

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

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