Home » excel » c# – Read An Excel File With OLEDB?

c# – Read An Excel File With OLEDB?

Posted by: admin May 14, 2020 Leave a comment


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);
                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();
                DataTable _DtTable = ds.Tables[0]; // or [ ds ]

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?

How to&Answers:

To add records to MyTable using ADO, you can use code similar to the following:

'Create a new connection object for Book1.xls
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=C:\Book1.xls;Extended Properties=Excel 8.0;"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
  " values ('Bill', 'Brown')"
conn.Execute "Insert into MyTable (FirstName, LastName)" & _
  " values ('Joe', 'Thomas')"

This is from MSDN: http://support.microsoft.com/kb/247412


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


You could look at some of the ways the database can consume Excelfiles