gpt4 book ai didi

c# - 读取一个大的 Excel 文档

转载 作者:可可西里 更新时间:2023-11-01 09:05:19 24 4
gpt4 key购买 nike

我想知道在 Excel 中读取单元格的最快方法是什么。我有一个包含 50000 行的 Excel 文件,我想知道如何快速阅读它。我只需要阅读第一列,使用 oledb 连接需要 15 秒。有没有更快的方法?

谢谢

最佳答案

这是一个依赖于使用 Microsoft.Office.Interop.Excel 的方法。

请注意:我使用的 Excel 文件只有一列包含 50,000 个条目的数据。

1) 用Excel打开文件,保存为csv,关闭Excel。

2) 使用StreamReader快速读取数据。

3) 将数据拆分回车换行后添加到字符串列表中。

4) 删除我创建的 csv 文件。

我使用 System.Diagnostics.StopWatch 为执行计时,该函数运行耗时 1.5568 秒。

public static List<string> ExcelReader( string fileLocation )
{
Microsoft.Office.Interop.Excel.Application excel = new Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
excel.Workbooks.Open(fileLocation);
workBook.SaveAs(
fileLocation + ".csv",
Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows
);
workBook.Close(true);
excel.Quit();
List<string> valueList = null;
using (StreamReader sr = new StreamReader(fileLocation + ".csv")) {
string content = sr.ReadToEnd();
valueList = new List<string>(
content.Split(
new string[] {"\r\n"},
StringSplitOptions.RemoveEmptyEntries
)
);
}
new FileInfo(fileLocation + ".csv").Delete();
return valueList;
}

资源:

http://www.codeproject.com/Articles/5123/Opening-and-Navigating-Excel-with-C

How to split strings on carriage return with C#?

关于c# - 读取一个大的 Excel 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com