1

我正在尝试构建一个将读取 excel 文件(.xlsx、.xls)的应用程序。不幸的是,OleDbDataAdapter.Fill() 的性能非常糟糕。我最多需要 2 分钟才能从文件中读取一条记录。

有关该文件的更多信息:

  1. 大小 - 257MB
  2. 列 - 104
  3. 行 - 1 000 000

我目前用来读取文件的代码:

    string conStr = string.Empty;
    string strQuery = string.Empty;
    switch (extension)
    {
        case ".xls": //Excel 97-03
            conStr = @"Provider=Microsoft.Jet.OleDb.12.0;Data Source=" + file_source + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';";
            strQuery = "SELECT " + col_no + " * FROM [" + workbook + "] ";
            break;

        case ".xlsx": //Excel 07
            //connection string to connect to the xlsx file
            conStr = @"Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + file_source + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;';";
            strQuery = "SELECT " + col_no + " * FROM [" + workbook + "] ";
            break;
    }
    DataTable tbl = new DataTable();
    OleDbDataAdapter ada = new OleDbDataAdapter(strQuery, conStr);
    ada.Fill(tbl);
    ada.Dispose();

    return tbl;

您的帮助将不胜感激!

谢谢!

4

1 回答 1

3

将大型数据集存储在 Excel 文件中是一个坏主意。我认为在 2 分钟内阅读 250MB Excel 文件是可以预料的。

我建议切换到任何数据库解决方案。但是如果你不能改变你的数据存储,你可以尝试使用 Excel COM 对象(虽然它需要在服务器机器上安装 Excel)。是泰迪·加兰的走道。

于 2014-07-21T13:05:39.667 回答