What is the analogue to Excel Interop's Worksheet.UsedRange.Rows in Spreadsheet Light?

£可爱£侵袭症+ 提交于 2019-12-07 22:36:32

问题


Using Excel Interop, you can get the count of rows in use by a sheet like so:

_xlSheet.UsedRange.Rows

(where "_xlSheet" is an Excel.Worksheet).

What is the equivalent in Spreadsheet Light?

You can add a worksheet like so:

var sl = new SLDocument();
. . .
sl.AddWorksheet("SheetsToTheWind");

...but how can you then access that worksheet to interrogate it for its used row count?


回答1:


After adding the worksheet it is active as well. That means that you can get the WorksheetStatistics from the method GetWorksheetStatistics. That statistics instance has a NumberOfRows property:

// NOTE: The information is only current at point of retrieval. 
var stats = sl.GetWorksheetStatistics();
var rowcount = stats.NumberOfRows;

If you want to to know the rowcount of all sheets you can do:

foreach(var name in sl.GetSheetNames())
{
    sl.SelectWorksheet(name);
    var stats = sl.GetWorksheetStatistics();
    var rowcount = stats.NumberOfRows;    
    Trace.WriteLine(String.Format("sheet '{0}' has {1} rows", name, rowcount));
}



回答2:


Adding to rene's answer:

Since accessing the Statistics' NumberOfRows property does not automagically update (you must call GetWorksheetStatistics() each time to get the up-to-date stats), I found it handy to write this helper method:

private int GetCurrentNumberOfRows()
{
    // This reference to "sl" assumes that you have declared "SLDocument sl;" and
    // instantiated it ("sl = new SLDocument();"), perhaps in your class' constructor
    var stats = sl.GetWorksheetStatistics();
    return stats.NumberOfRows;
}

..and then call it as needed:

int lastRow = GetCurrentNumberOfRows();


来源:https://stackoverflow.com/questions/36481802/what-is-the-analogue-to-excel-interops-worksheet-usedrange-rows-in-spreadsheet

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