2

I mostly write number-crunching programs using Visual Studio C# (2019) where I am simply taking input data, calculating results and displaying it. No complicated Network or Internet programming. Think first or second college level programming coarse from the early 1990's.

For inputs I was reading in data from an excel file using the following directive:

using Excel = Microsoft.Office.Interop.Excel;

This proved to be very slow when executing the program. I then learned this way of accessing an Excel file is no longer supported and has been superseded by Open XML SDK. Please see the following link to the Microsoft Dev Center page:

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

For what I want to do the Document Object Model(DOM) approach seems most appropriate for the thousands of individual excel cells I want to read as input data. However, the Microsoft Dev Center is certainly not the most user-friendly resource and the code example provided for reading an Excel file using this DOM approach is writing to a console which I'm not using. I never did get my code to work.

Long and short of it is, I got my code working using the GetCellValue Method:

https://learn.microsoft.com/en-us/office/open-xml/how-to-retrieve-the-values-of-cells-in-a-spreadsheet

However, this 'GetCellValue' method is still taking way too long. I need to read in thousands or tens of thousands of Excel input data cells in seconds or fractions of seconds not 20 seconds to a minute.

I think if I had an example of the DOM method reading in Excel data to an Array Variable (instead of writing to the console) it would help. Can anyone provide an example of such code?

Below I have included my code example where I modified the DOM approach code copied from the Microsoft Office Dev Center to write values from a source Excel File to a DataGrid instead of the Console used by the Dev Center code:

C#


    // The DOM approach.
    // Note that the code below works only for cells that contain numeric values.
    // 
            public void ReadExcelFileDOM(string fileName)
            {
                using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
                {
                    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
                    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
    
                    DataGridView_Vessel.Rows.Clear();
                    DataGridView_Vessel.Refresh();
    
                    string text;
                    int File_Row = 0;
                    int File_Cell = 0;
                    foreach (Row r in sheetData.Elements<Row>())
                    {
                        DataGridView_Vessel.Rows.Add();
    
                        foreach (Cell c in r.Elements<Cell>())
                        {
                            if (c.CellValue == null)
                            {
                                File_Cell++;
                                //continue;
                            }
                            else
                            {
                                text = c.CellValue.Text;
                                if(File_Cell<12)
                                {
                                    DataGridView_Vessel.Rows[File_Row].Cells[File_Cell].Value = text;
                                }
                                File_Cell++;
                            }   
                        }
                        File_Row++;
                    }
                    //Console.WriteLine();
                    //Console.ReadKey();
                }
            }


    

XGI_451
  • 21
  • 3
  • 2
    Can you share the Open XML code you have tried? – Edney Holder Jul 05 '20 at 04:08
  • 3
    There are libraries such as ClosedXml and ExcelDataReader on Nuget to read Excel files without having to call Open XML directly. – Jesús López Jul 05 '20 at 06:39
  • We need to see a [mcve] to help you. As for Excel Interop, performance can be greatly improved by querying the entire range of cells at once, see [Microsoft.Office.Interop.Excel really slow](https://stackoverflow.com/a/3989452/3744182). There is also OLEDB, see [Performance of OLEDB to read Excel](https://stackoverflow.com/a/11352167). For more alternatives see [How to get OleDb for reading excel in asp.net core project](https://stackoverflow.com/q/40438377). – dbc Jul 06 '20 at 15:06
  • Also, if your code works, https://codereview.stackexchange.com/ may be a better forum for [open-ended feedback in the following areas... Performance](https://codereview.stackexchange.com/help/on-topic). – dbc Jul 06 '20 at 15:07
  • Code example has been added as requested. – XGI_451 Jul 15 '20 at 02:06

0 Answers0