解析excel文件时,OpenXML读取性能太慢

时间:2017-05-26 13:27:45

标签: c# asp.net excel openxml openxml-sdk

我正在使用C#和OpenXML(SAX)阅读excel文件。文件大小介于5-10 MB之间,有5-6张。每张纸的行数在25-100K之间变化。

我使用以下代码来获取数据。它读取大约每秒100行。相比之下,Apache POI能够同时读取一千行。

我预计会有更好的结果,因为这两种产品都来自微软。我做错了吗?

using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Open(filename, true))
{
    WorkbookPart workbookPart = excelDoc.WorkbookPart;
    WorksheetPart mainSheet = (WorksheetPart)workbookPart.GetPartById(sheetIds[0].ToString());
    OpenXmlReader reader = OpenXmlReader.Create(mainSheet);

    //CellType c;
    SharedStringTable t = workbookPart.SharedStringTablePart.SharedStringTable;

    while (reader.Read())
    {
        if (reader.ElementType == typeof(Row))
        {
            reader.ReadFirstChild();

            do
            {
                if (reader.ElementType == typeof(Cell))
                {
                    Cell c = (Cell)reader.LoadCurrentElement();

                    string cellValue;

                    if (c.DataType != null && c.DataType == CellValues.SharedString)
                    {
                        int index = int.Parse(c.CellValue.InnerText);
                        SharedStringItem ssi = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(int.Parse(c.CellValue.InnerText));
                        cellValue = ssi.Text.Text;
                    }
                    else
                    {
                        cellValue = c.CellValue.InnerText;
                    }

                    //Console.Out.Write("{0}: {1} ", c.CellReference, cellValue);
                }
            }
            while (reader.ReadNextSibling());
        }
    }
}

0 个答案:

没有答案