简体   繁体   English

使用更好的方式读取 excel 数据

[英]using better way to read the excel data

Right now i am getting the data from excel and looping through the rows and based on condition i am processing the results like storing the result in an object for further processing.现在,我正在从 excel 获取数据并遍历行并根据条件处理结果,例如将结果存储在 object 中以供进一步处理。

The excel sheet is about 20 MB and the record count is closely to 7000 and i am using open xml to grab the data from excel file like as mentioned in below code. excel 表约为 20 MB,记录数接近 7000,我正在使用打开的 xml 从 excel 文件中获取数据,如下面的代码所示。

        string filePath = @"C:\weather-Data\DesignConditions_p.xlsx";
        using FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        using SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false);
        WorkbookPart workbookPart = doc.WorkbookPart;
        SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
        SharedStringTable sst = sstpart.SharedStringTable;

        Sheet firstSheet = workbookPart.Workbook.Descendants<Sheet>().First();
        Worksheet sheet = ((WorksheetPart)workbookPart.GetPartById(firstSheet.Id)).Worksheet;

        var rows = sheet.Descendants<Row>();
        var weatherDataList = new List<WeatherStation>();
        foreach (Row row in rows.Skip(5)) // it is taking almost more than 60 minutes to process and enter into the if loop below (country.Equals("USA"))
        {
            var weatherData = new WeatherStation();
            string country = GetCellValue(filePath, "Annual", $"B{row.RowIndex.ToString()}");
            if (country.Equals("USA"))
            {
                weatherData.CountryAbbreviation = country;
                weatherData.StateAbbreviation = GetCellValue(filePath, "Annual", $"C{row.RowIndex.ToString()}");
                weatherData.Number = GetCellValue(filePath, "Annual", $"E{row.RowIndex.ToString()}");
                ......
                .......
            }
        }

Could any one please point me in the right direction to optimize the processing time while reading the data from excel, I am using.Net Core for this application在从 excel 读取数据时,谁能指出我正确的方向以优化处理时间,我正在为这个应用程序使用.Net Core

Thanks in advance.提前致谢。

You could use the 'SAX' approach, that way you're reading the file in parts so processing and IO could be faster.:您可以使用“SAX”方法,这样您可以部分读取文件,以便处理和 IO 可能更快。:

// The SAX approach.
    static void ReadExcelFileSAX(string fileName)
    {
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
        {
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

            OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
            string text;
            while (reader.Read())
            {
                if (reader.ElementType == typeof(CellValue))
                {
                    text = reader.GetText();
                    Console.Write(text + " ");
                }
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }

https://docs.microsoft.com/en-us/office/open-xml/how-to-parse-and-read-a-large-spreadsheet https://docs.microsoft.com/en-us/office/open-xml/how-to-parse-and-read-a-large-spreadsheet

Other than that you could look for a library/nuget package that has some faster reading, because i see no impact-full ways to tune this code more.除此之外,您可以寻找具有更快阅读速度的库/nuget package,因为我看不到可以更有效地调整此代码的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM