Optimal way to Read an Excel file (.xls/.xlsx)

后端 未结 7 1793
一生所求
一生所求 2020-11-27 12:01

I know that there are different ways to read an Excel file:

  • Iterop
  • Oledb
  • Open Xml SDK

C

7条回答
  •  再見小時候
    2020-11-27 12:31

    Try to use Aspose.cells library (not free, but trial is enough to read), it is quite good

    Install-package Aspose.cells

    There is sample code:

    using Aspose.Cells;
    using System;
    
    namespace ExcelReader
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Replace path for your file
                readXLS(@"C:\MyExcelFile.xls"); // or "*.xlsx"
                Console.ReadKey();
            }
    
            public static void readXLS(string PathToMyExcel)
            {
                //Open your template file.
                Workbook wb = new Workbook(PathToMyExcel);
    
                //Get the first worksheet.
                Worksheet worksheet = wb.Worksheets[0];
    
                //Get cells
                Cells cells = worksheet.Cells;
    
                // Get row and column count
                int rowCount = cells.MaxDataRow;
                int columnCount = cells.MaxDataColumn;
    
                // Current cell value
                string strCell = "";
    
                Console.WriteLine(String.Format("rowCount={0}, columnCount={1}", rowCount, columnCount));
    
                for (int row = 0; row <= rowCount; row++) // Numeration starts from 0 to MaxDataRow
                {
                    for (int column = 0; column <= columnCount; column++)  // Numeration starts from 0 to MaxDataColumn
                    {
                        strCell = "";
                        strCell = Convert.ToString(cells[row, column].Value);
                        if (String.IsNullOrEmpty(strCell))
                        {
                            continue;
                        }
                        else
                        {
                            // Do your staff here
                            Console.WriteLine(strCell);
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题