How to read an Excel spreadsheet in c# quickly

前端 未结 5 1104
被撕碎了的回忆
被撕碎了的回忆 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:41

    Hi I found a very much faster way.

    It is better to read the entire data in one go using "get_range". This loads the data into memory and I can loop through that like a normal array.

    Microsoft.Office.Interop.Excel.Range range = gXlWs.get_Range("A1", "F188000");
    object[,] values = (object[,])range.Value2;
    int NumRow=1;
    while (NumRow < values.GetLength(0))
    {
        for (int c = 1; c <= NumCols; c++)
        {
            Fields[c - 1] = Convert.ToString(values[NumRow, c]);
        }
        NumRow++;
    }
    

提交回复
热议问题