我有一个包含大约150万条记录的excel文件。我的目的是从excel文件中读取前100 000条记录到我的数据表(这里是c#datatable,即dt)对这些记录进行一些处理,然后读取下一条100000条记录,依此类推,直到我获取excel文件中的所有记录。 (每次只有100000行)
目前,我使用以下代码
获取所有记录 public bool ReadDataFile(string filePath)
{
string strConnString = null;
string sheetName = null;
string ErrSheetName = null;
DataSet dsEx = new DataSet();
strConnString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filePath + ";Mode=ReadWrite;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConnString);
try
{
conn.Open();
DataTable DtSheetName = new DataTable();
DataTable dt = new DataTable();
DtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i <= DtSheetName.Rows.Count - 1; i++)
{
sheetName = DtSheetName.Rows[i]["TABLE_NAME"].ToString();
if (sheetName.Length >= 4)
{
conn.Close();
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn))
{
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = cmd;
try
{
da.Fill(dt);
dt.TableName = sheetName.Replace("$", "");
}
catch (Exception ex)
{
ErrSheetName = ErrSheetName + "," + sheetName.Replace("$", "");
}
}
}
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ReadExcel()");
return false;
}
finally
{
if (!string.IsNullOrEmpty(ErrSheetName))
{
MessageBox.Show("Error in Data Of these files [" + ErrSheetName + "] Some Data may be lost !", "ReadExcel()");
}
conn.Close();
GC.Collect();
}
}