使用 OLEDB 读取 Excel 文件?
您好,我正在使用 oledb 读取 Excel 文件(该文件有 100000 行)。我必须快速读取文件。
string conn;
conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
("Data Source=" + _filename + ";" +
"Extended Properties=\"Excel 12.0;\""));
OleDbConnection oleDBCon = new OleDbConnection(conn);
oleDBCon.Open();
DataTable dt = oleDBCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string excelsheetname = dt.Rows[0].ItemArray[2].ToString();
string SSQL = "SELECT * from [" + excelsheetname + "]";
OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
DataSet ds = new DataSet();
oleDA.Fill(ds);
DataTable _DtTable = ds.Tables[0]; // or [ ds ]
oleDBCon.Close();
然后在 _DtTable 中使用 for 循环将这些单元格插入到数据库中。如何快速读取这个非常大的 Excel?并插入数据库?我使用了 Parallel.For 但这对我来说不是真正的解决方案..有什么想法吗?
Hi I am reading an excel file with oledb(The file has 100000 rows). I must read file quickly.
string conn;
conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
("Data Source=" + _filename + ";" +
"Extended Properties=\"Excel 12.0;\""));
OleDbConnection oleDBCon = new OleDbConnection(conn);
oleDBCon.Open();
DataTable dt = oleDBCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string excelsheetname = dt.Rows[0].ItemArray[2].ToString();
string SSQL = "SELECT * from [" + excelsheetname + "]";
OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
DataSet ds = new DataSet();
oleDA.Fill(ds);
DataTable _DtTable = ds.Tables[0]; // or [ ds ]
oleDBCon.Close();
and then in _DtTable with a for loop I am inserting these cells to DB.. How can I read this very large excel quickly? And insert to DB? I used Parallel.For but it is not true solution for me.. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使用 ADO 将记录添加到 MyTable,您可以使用类似于以下的代码:
这来自 MSDN:http:// /support.microsoft.com/kb/247412
To add records to MyTable using ADO, you can use code similar to the following:
This is from MSDN: http://support.microsoft.com/kb/247412
利用 SQL 的 OpenXML 批量插入数据,这对您来说更重要,
这是我为相同工作所做的代码:使用C# DataTable和SQL Server OpenXML函数批量插入数据
Make use of OpenXML of SQL to insert data in bulk which do fater work for you
here is code did by me for same work : Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function
您可以查看数据库使用的一些方式 Excel文件
You could look at some of the ways the database can consume Excelfiles