【问题标题】:How to process large excel files?如何处理大的excel文件?
【发布时间】:2013-12-14 14:13:42
【问题描述】:

我在使用数据阅读器上传大型 Excel 文件 (300mb+) 时遇到问题。使用此代码,我打开 excel 文件并分别加载每一行。使用断点我注意到一条语句需要 30 秒以上。内存使用量也在稳步增长。 指定 ExecuteReader() 方法的 CommandBehavior 参数(例如 SequentialAccess)无效。

我在这里做错了什么?是否有其他处理大型(excel)文件的方法?

const string inputFilePath = @"C:\largefile.xlsx";
const string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;\";Data Source=" + inputFilePath;
using (var connection = new OleDbConnection(connectionString))
{
    connection.Open();
    var command = new OleDbCommand("largesheet$", connection) {CommandType = CommandType.TableDirect};
    var reader = command.ExecuteReader(); // <-- Completely loads file/sheet into memory
    while (reader.HasRows)
    {
        reader.Read();
    }
    connection.Close();
}

【问题讨论】:

  • "(300mb+)" - 你的问题。你不能在某种程序中导入 Excel 文件吗?比如数据库?
  • 那么唯一的办法就是拆分文件单独上传?
  • 也许使用 OpenXML 可以更快地解析文件,但 3000MB 仍然非常大。
  • 保存为 Excel 二进制工作表,这可能是一个好方法吗?

标签: c# excel datareader large-data


【解决方案1】:

你可以尝试用这个将文件加载到内存中吗:

Stream exportData = new MemoryStream(byte[] fileBuffer);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2015-10-30
    • 2020-08-10
    • 2013-01-26
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多