0

从 Excel 2010 读取大量数据时出现问题

我使用下面的代码来读取数据:

public static DataTable GetExcelData(string filePath, string extension)
        {
            try
            {
                string conStr = "";
                switch (extension)
                {
                    case ".xls": //Excel 97-03
                        //conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        //conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR={YES}'";
                        conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
                        break;
                    case ".xlsx": //Excel 07
                        //conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
                        break;
                }

                //conStr = String.Format(conStr, filePath, true);
                OleDbConnection connExcel = new OleDbConnection(conStr);
                OleDbCommand cmdExcel = new OleDbCommand();
                OleDbDataAdapter oda = new OleDbDataAdapter();
                DataTable dt = new DataTable();
                cmdExcel.Connection = connExcel;

                //Get the name of First Sheet
                log.Debug("Excel ConnectionString = " + conStr);
                connExcel.Open();
                log.Debug("Open Excel connection ok");
                DataTable dtExcelSchema;
                dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                connExcel.Close();
                log.Debug("SHEET NAME = " + SheetName);
                //Read Data from First Sheet
                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                oda.SelectCommand = cmdExcel;
                oda.Fill(dt);
                connExcel.Close();

                return dt;
            }
            catch(Exception ex)
            {
                log.Debug("GetExcelData Error:" + ex.ToString());
                return null;
            }
        }

我的 ASP.NET MVC 2.0 项目在使用小型 Excel 文件和大型 Excel 文件的 Visual Studio 2010 上运行时可以正常工作。

但是,当我部署到 IIS 7.5 (windows 7 pro) 时,我的网站仅适用于小型 Excel 文件。

使用大的excel文件,它会引发以下错误:

GetExcelData Error:System.Data.OleDb.OleDbException (0x80004005): External table is not in the expected format.
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open() 

(在IIS 7.5上,我的网站上传大文件Excel成功)

我怎样才能解决这个问题?

4

1 回答 1

0

可能需要更改 web.config 以允许超过 4MB 的较大文件。您可以将以下内容添加到 web.config 以允许这样做,

<configuration>
    <system.web><!-- maxRequestLength (KB) -->
        <httpRuntime maxRequestLength="10000" executionTimeout="110"/>
    </system.web>
</configuration>

这可能会有所帮助,

超出最大请求长度

我使用以下连接字符串,其中“{0}”是文件路径,对于 .xls 文件,

strConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};Extended Properties= ""Excel 12.0 Xml;HDR=YES;IMEX=1"""

对于我使用的 .xlsx 文件,

strConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES""";

如果您设置了 web.config,它不会是文件的大小。我会尝试将文件保存为 97-2003 工作簿 .xls 文件并使用相关的连接字符串。如果您将其托管在服务器上,则服务器将需要处理文件所需的 Office 驱动程序。

UPDATE 将 HDR 设置为 No 并遍历每一行以消除标题行。这将避免在检查列的数据类型时发生的数据类型问题。

于 2013-12-08T16:40:20.107 回答