我从Interop
库切换到OpenXML
,因为我需要读取大型Excel
文件。在此之前,我可以使用:
worksheet.UsedRange.Rows.Count
为了获取工作表上带有数据的行数,我使用了这些信息来制作进度条。在OpenXML中,我不知道如何获得有关工作表的同样信息。现在我拥有的是这段代码:
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(path, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
int row_count = 0, col_count;
// here I would like to get the info about the number of rows
foreach (Row r in sheetData.Elements<Row>())
{
col_count = 0;
if (row_count > 10)
{
foreach (Cell c in r.Elements<Cell>())
{
// do some stuff
// update progressbar
}
}
row_count++;
}
}