How to read an Excel spreadsheet in c# quickly

前端 未结 5 1106
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 06:13

I am using Microsoft.Office.Interop.Excel to read a spreadsheet that is open in memory.

gXlWs = (Microsoft.Office.Interop.Excel.Worksheet)gXlApp.ActiveWorkbo         


        
5条回答
  •  粉色の甜心
    2020-12-05 06:38

    Use the OleDB Method. That is the fastest as follows;

    string con =
      @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;" + 
      @"Extended Properties='Excel 8.0;HDR=Yes;'";    
    using(OleDbConnection connection = new OleDbConnection(con))
    {
        connection.Open();
        OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection); 
        using(OleDbDataReader dr = command.ExecuteReader())
        {
             while(dr.Read())
             {
                 var row1Col0 = dr[0];
                 Console.WriteLine(row1Col0);
             }
        }
    }
    

提交回复
热议问题