我正在尝试获取DataTable
一个 .xls 文件。如果我使用大小为 25kb 的 .xls 文件运行以下代码,它可以正常工作,但如果我加载更大大小的文件 (7,52MB),它就不起作用。
string filenamePath = System.IO.Path.Combine(Server.MapPath("/Uploads"), FileUpload1.FileName);
FileUpload1.SaveAs(filenamePath);
string[] validFileTypes = { "xls", "xlsx"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isValidFile = false;
string fileName = FileUpload1.FileName;
for (int i = 0; i < validFileTypes.Length; i++)
{
if (ext == "." + validFileTypes[i])
{
isValidFile = true;
break;
}
}
if (isValidFile)
{
DataTable dt = ConvertXLSTpXLM.convertXLSToDb(filenamePath, "ELEMENT", "ELEMENTS", true, fileName);
}
这是convertXLSToDb
方法
public static DataTable convertXLSToDb(string filePath, string firstElement, string secondElement, bool hasHeaders, string filename)
{
DataTable dtexcel = new DataTable();
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xls")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataRow schemaRow = schemaTable.Rows[0];
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
dtexcel.Locale = CultureInfo.CurrentCulture;
daexcel.Fill(dtexcel);
}
conn.Close();
return dtexcel;
}
没有发生错误,但在“daexcel.Fill(dtexcel);”上 页面无限运行。