简体   繁体   English

如何快速阅读c#中的Excel电子表格

[英]How to read an Excel spreadsheet in c# quickly

I am using Microsoft.Office.Interop.Excel to read a spreadsheet that is open in memory. 我正在使用Microsoft.Office.Interop.Excel来读取内存中打开的电子表格。

gXlWs = (Microsoft.Office.Interop.Excel.Worksheet)gXlApp.ActiveWorkbook.ActiveSheet;
int NumCols = 7;
string[] Fields = new string[NumCols];
string input = null;
int NumRow = 2;
while (Convert.ToString(((Microsoft.Office.Interop.Excel.Range)gXlWs.Cells[NumRow, 1]).Value2) != null)
{
    for (int c = 1; c <= NumCols; c++)
    {
        Fields[c-1] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)gXlWs.Cells[NumRow, c]).Value2);
    }
    NumRow++;

    //Do my other processing
}

I have 180,000 rows and this turns out be very slow. 我有180,000行,结果非常慢。 I am not sure the "Convert" is efficient. 我不确定“转换”是否有效。 Is there anyway I could do this faster? 反正我还能做得更快吗?

Moon 月亮

Hi I found a very much faster way. 嗨,我找到了一个非常快的方法。

It is better to read the entire data in one go using "get_range". 最好使用“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++;
}

There are several options - all involve some additional library: 有几个选项 - 都涉及一些额外的库:

  • OpenXML 2.0 (free library from MS) can be used to read/modify the content of an .xlsx so you can do with it what you want OpenXML 2.0 (来自MS的免费库)可用于读取/修改.xlsx的内容,因此您可以根据需要使用它

  • some (commercial) 3rd-party libraries come with grid controls allowing you to do much more with excel files in your application (be it Winforms/WPF/ASP.NET...) like SpreadsheetGear , Aspose.Cells etc. 一些(商业)第三方库带有网格控件,允许您在应用程序中使用excel文件(如Winforms / WPF / ASP.NET ...),如SpreadsheetGearAspose.Cells等。

I am not sure the "Convert" is efficient. 我不确定“转换”是否有效。 Is there anyway I could do this faster? 反正我还能做得更快吗?

What makes you believe this? 是什么让你相信这个? I promise you that Convert.ToString() is the most effective method in the code you posted. 我保证Convert.ToString()是您发布的代码中最有效的方法。 Your problem is that your looping through 180,000 records in an excel document... 您的问题是您在Excel文档中循环180,000条记录...

You could split the work up since you know the number of row this is trival to do. 您可以拆分工作,因为您知道要执行的行数。

Why are you coverting Value2 to a string exactly? 为什么要将Value2完全转换为字符串?

Use the OleDB Method. 使用OleDB方法。 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);
         }
    }
}

I guess it's not the Convert the source of "slowing"... 我想这不是转换“减速”的来源......

Actually, retrieving cell values is very slow. 实际上,检索单元格值非常慢。

I think this conversion is not necessary: 我认为这种转换不是必要的:

(Microsoft.Office.Interop.Excel.Range)gXlWs

It should work without that. 它应该没有它。

And you can ask directly: 你可以直接问:

gXlWs.Cells[NumRow, 1].Value != null

Try to move the entire range or, at least, the entire row to an object Matrix and work with it instead of the range itself. 尝试将整个范围或至少整行移动到对象Matrix并使用它而不是范围本身。

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

相关问题 使用C#从Excel电子表格中读取/写入 - To read/write from excel spreadsheet using C# 如何使用C#中安装的Excel外接程序创建Excel电子表格? - How to create an excel spreadsheet with an excel add-in installed in c#? 如何在c#中快速读取二进制文件? (ReadOnlySpan vs MemoryStream) - How to read a binary file quickly in c#? (ReadOnlySpan vs MemoryStream) 将Excel电子表格移植到C# - Port Excel spreadsheet to c# 如何将C#对象列表导出到Excel电子表格? - How do I export a C# object list to an excel spreadsheet? 如何使用 Interop 从复杂的 C# model 电子表格转换为 Excel 电子表格 - How to convert from complex C# model to Excel spreadsheet with Interop 如何使用NPOI C#大写Excel电子表格中的所有单词 - How to uppercase all the words in excel spreadsheet using NPOI C# 如何为C#生成的Excel电子表格设置工作表名称? - How to set the sheet name for a C# generated Excel spreadsheet? 如何在C#中修改Excel电子表格,另存为其他名称并删除原始电子表格 - How do I in C# modify an Excel spreadsheet, save it as another name and delete the original spreadsheet C# Gembox 电子表格 - 读取引用另一个 Excel 文件的单元格值 - C# Gembox Spreadsheet - Read cell value which references to another excel file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM