Read An Excel File With OLEDB? - c#

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?

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')"
conn.Close
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

Related

Reading Multiples excel tables having variable rows lenght in the same Worksheet using c#

So i have an excel file containing a worksheet with 4 tables
My excel worsheet
i want to be able to get all of those tables to separate dataTable knowing that the lenght of the rows can change (rows can be added or deleted)
I've tried using oleDb which work just fine with the first table in the worksheet but when i try to get the second one (by specifying the starting row) it give me the second table with all the others tables in one datatable :/
here is my code :
public static System.Data.DataTable getTableSelection(string file, string sheet, string starting = "", string finish = "")
{
string ConStr;
string HDR;
HDR = "YES";
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ file + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection cnn = new OleDbConnection(ConStr);
string query = "select * from ["
+ sheet + "$" + (starting != "" ? starting + ":" + finish : "") + "]";
Console.WriteLine(query);
OleDbCommand oconn = new OleDbCommand(query, cnn);
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
System.Data.DataTable dt = new System.Data.DataTable();
adp.Fill(dt);
cnn.Close();
return dt;
}
Sorry for my bad english , hope you understand !!
love StackOverflow
So i found a solution which consist on joining all my tables to get a all the fields i need and then transform it to a datatable by using oleDb query ; my query look like this : select * from (([Sheet1$A:D9] INNER JOIN [Sheet1$A14:D21] ON [Sheet1$A:D9].primaryKey=[Sheet1$A14:D21].primaryKey) INNER JOIN [Sheet2$A26:M33] ON [Sheet2$A26:M33].primaryKey=[Sheet1$A14:D21].primaryKey)
but still , i am not being able to get the table without specifying where it should end :/

.Dat file to dataset

I have a .dat file that I need to import into a datatable. I am using .net framework 4.5 and it delimited by ~. How do I import this? I tried
string mySelectQuery = "SELECT * FROM " + fileName;
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileLocation + ";Extended Properties=\"text;HDR=YES;FMT=Delimited\"");
OleDbDataAdapter dsCmd = new OleDbDataAdapter(mySelectQuery, myConnection);
//Fill the DataSet object
dsCmd.Fill(myData, "Packets");
Please help

in C# using oledb connection string how to Convert Scientific Notation to text strings?

I have a set of data that is downloaded as a Excel file using OLEDB connection string like so:
4552
3.00E+03
3.00E+22
3F45
3.00E+99
DD56677
37
Excel automatically thinks that 3E03, 3E22 and 3E99 are numbers and makes them look like above..
how to get it as a string ?
my code is
DataTable dataTable = new DataTable();
strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + strFilePath + ";"
+ "Extended Properties='Excel 8.0;HDR=" + header + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text'";
OleDbConnection connection = new OleDbConnection(strExcelConn);
using (OleDbCommand command = new OleDbCommand())
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
command.Connection = connection;
//Check if the Sheet Exists
connection.Open();
DataTable dtExcelSchema;
//Get the Schema of the WorkBook
dtExcelSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connection.Close();
connection.Open();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
command.CommandText = "SELECT * From [" + SheetName + "]";
adapter.SelectCommand = command;
adapter.Fill(dataTable);
connection.Close();
}
please can any one help me to get this colum as a string
Have a look at this StackOverflow question entitled How to convert a string containing an exponential number to decimal and back to string.
They use the decimal.Parse(someMoneyVar, NumberStyles.Any) method to do the conversion.
Select Excel Columns by name in your query and cast your required columns to a string using CStr(<columnName>)

Missing rows reading generated Excel file

I am creating an Excel File using OleDB, it works perfectly, next, I add rows to the file using directly in Office Excel , after, when i try read the file using OleDB again, the rows i added manually does not appear, only the columns and rows i wrote when i created the file.
Here is the code:
//Creation of the file
String connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Mode=ReadWrite; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=0'";
OleDbConnection oledbconnection;
oledbconnection = new OleDbConnection(connectionString);
oledbconnection.Open();
String query = "CREATE TABLE [Sheet1] (....) ";
OleDbCommand cmd = new OleDbCommand(query, oledbconnection);
cmd.ExecuteNonQuery();
oledbconnection.Close();
it works perfectly, the file is created correctly, then, when im trying read the file after modify it, I'm doing this
String connectionStringRead = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Extended Properties= 'Excel 8.0; HDR=Yes; IMEX=1'";
OleDbConnection oledbconnection;
oledbconnection = new OleDbConnection(connectionStringRead);
oledbconnection.Open();
DataTable table = new DataTable();
String sheetName;
sheetName = oledbconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "]", oledbconnection);
adapter.Fill(table);
oledbconnection.Close();
return table;
The table contains ONLY the original information, not the rows I added using MS Excel.
I don't understand this, can you help me please ?
thanks in advance
Never try like that before, but if
sheetName = "Sheet1" '-------> or whatever
It should be
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" + sheetName + "$]", oledbconnection);

c# Excel skipping first row?

I'm importing an xls file using OleDbCommand to a ds. Problem I'm having is during the foreach on my ds its skipping for first row. I can't figure out why. Any suggestions?
cmd.CommandText = string.Format("SELECT * FROM [{0}$]", worksheetName);
conn.Open();
var adapter = new OleDbDataAdapter();
var ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
var table = ds.Tables[0];
foreach(DataRow row in table.Rows){ // rest of my code }
Change the connection string (as mentioned in comment) from:
string cnn = string.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"data source={0}{1}{2};" +
"Extended Properties=Excel 8.0;",
fileLocation, fileName, fileExtension);
to:
string cnn = string.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"data source={0}{1}{2};" +
"Extended Properties=Excel 8.0;HDR=No",
fileLocation, fileName, fileExtension);
Check your connection string. Most likely it contains:
HDR=Yes
which indicates that first row is a header
Maybe you've told it to skip first row. I remember a property that sounds something like FirstRowIsHeader. I think if that is set to true, then it skips. this can be changed at point you create your connection

Categories