从 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成功)
我怎样才能解决这个问题?