我正在尝试使用SAX方法处理大型Excel文件。在我的场景中,用户将映射电子表格中的一些列,我们将根据单元格的位置和单元格的值从每行获取数据。
这个过程正在发挥作用,但我希望它花费更少的时间。看来我做错了吗?
private string GetCellValue(Cell c, WorkbookPart workbookPart)
{
if (c.DataType != null && c.DataType == CellValues.SharedString)
{
return workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>()
.ElementAt(int.Parse(c.CellValue.InnerText)).Text.Text;
}
return c.CellValue != null ? c.CellValue.InnerText : string.Empty;
}
private void ProcessDocument()
{
using (var spreadsheetDocument = SpreadsheetDocument.Open(model.FileName, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
var reader = OpenXmlReader.Create(worksheetPart);
while (reader.Read())
{
if (reader.ElementType != typeof(Row))
{
continue;
}
rowCount++;
reader.ReadFirstChild();
var values = new ParcelValueImportValues();
do
{
if (reader.ElementType != typeof(Cell) || rowCount <= 1)
{
continue;
}
var c = (Cell)reader.LoadCurrentElement();
var location = GetCellLocation(c.CellReference);
var cellValue = GetCellValue(c, workbookPart);
if (location.Equals(hist.AssessmentRatio))
{
values.AssessmentRatio = cellValue;
}
else if (location.Equals(hist.AvImprovements))
{
values.AvImprovements = cellValue;
}
else if (location.Equals(hist.AvLand))
{
values.AvLand = cellValue;
}
else if (location.Equals(hist.FmvLand))
{
values.FmvLand = cellValue;
}
else if (location.Equals(hist.FmvImprovements))
{
values.FmvImprovements = cellValue;
}
else if (location.Equals(hist.ParcelNumber))
{
values.ParcelNumber = cellValue;
}
} while (reader.ReadNextSibling());
}
}
}