first blank row in excel

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 05:16:12

问题


Is there a way to find first available blank row in excel sheet using vsto or c# ? I have been searching for hours but cant find any thing related to it. Any help in this regard will be greatly appreciated


回答1:


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):

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.

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.




回答2:


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




回答3:


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.

Have a look at this question for a possible solution: How to find the Next blank row in Excel sheet programatically using C#
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.



来源:https://stackoverflow.com/questions/10752971/first-blank-row-in-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!