How to count empty rows when reading from Excel - c#

I'm using OLEDB to connect and read through data from an Excel spreadsheet. I have IMEX="1" and everything works ok. My problem is the sheets I'm reading from may start with several empty rows and the number of empty rows is important. For example, if I was reading a 5x5 grid like:
- - - - -
- - - - -
2 - 3 3 8
- - - - -
- - 5 2 2
where '-' represents an empty cell. The fact that the first two rows are empty is important. The size of the grid is dynamic. My code appears to be ignoring the first empty rows. But deals with the empty row at line 4 ok.
How can I count the number of empty rows at the start of an Excel sheet using OLEDB?
I'm restricted to using OLEDB, I wouldn't if I didn't have to ;-)
using (var adapter = new OleDbDataAdapter("SELECT * FROM [" + worksheetName + "]", connString)) {
var ds = new DataSet();
adapter.Fill(ds, "FareChart");
table = ds.Tables["FareChart"];
}
Connection string:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Windows\\TEMP\\e1842f90-74a7-42f2-a6fa-208396a1072e;Extended Properties=\"Excel 8.0;IMEX=1;HDR=No\""
UPDATE
Specifying '.xls' as the file extension in the connection string fixed this issue and correctly reads the empty rows at the start.

I think your problem is with your connection string. I tested the below code and it worked for me:
DataSet Contents = new DataSet();
using (OleDbDataAdapter adapter = new OleDbDataAdapter("select FirstName,LastName,Email,Mobile from [" + mySheet + "]", connection))
{
adapter.Fill(Contents,"MyTable");
}
foreach (DataRow content in Contents.Tables["MyTable"].Rows)
{
if (content[0].ToString() == "" && content[0].ToString() == "" && content[0].ToString() == "" && content[0].ToString() == "")
{
Console.WriteLine("Empty Row");
}
else
{
Console.WriteLine(content[0] + " | " + content[1] + " | " + content[2] + " | " + content[3]);
}
}
My Connection String is:
string cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Untitled 1.xls\";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

check below code :It will return the empty rows..
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 8.0;HDR=NO;IMEX=1\";"); /*for office 2007 connection*/
conn.Open();
string strQuery = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataTable ExcelToDataTable = new System.Data.DataTable();
adapter.Fill(ExcelToDataTable);
DT = ExcelToDataTable.Copy();
int count = DT.Rows.Cast<DataRow>().Where(row => row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).ToList().Count();

As stated by #Knvn
You need to specifiy the file extension .xls with the file name in your connection string.

Related

"Syntax error in FROM clause" Excel in c#

i have an Excel file i want to raed it in c# using the OleDb like the following code:
string sheetName = "sheet1";
try
{
string stringConnection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=excelfile.xls;Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;MAXSCANROWS=0';";
OleDbConnection OleDbConnection_ = new OleDbConnection(stringConnection);
OleDbCommand OleDbCommand_ = new OleDbCommand("select * from [" + sheetName + "$]; ", OleDbConnection_);
OleDbConnection_.Open();
DataTable DataTable_ = new DataTable();
DataTable_.Load(OleDbCommand_.ExecuteReader(), LoadOption.OverwriteChanges);
OleDbConnection_.Close();
}
catch (Exception)
{
throw;
}
Everything is working fine, just when i change the SheetName Value to " topos .architectures. bureaux " like the name in the xls file that i have, an exception was shown:
Syntax error in FROM clause.
what i messed it up here, thank you.
I think you just need to remove the $ in "select * from [" + sheetName + "$]; "
The $ is indeed use in references in Excel itself, but I think is does not apply to the OleDB command syntax
See this question for reference: Reading from excel using oledbcommand
The problem was in the dots in the Sheet name, the dots should be an #.
Like that:
"select * from ['" + sheetName.Replace('.', '#') + "$'];"

Copy excel table into DataTable faster

I am trying to copy an excel sheet with 1 Million records into a Data Table. Unfortunately it takes about 47 seconds to complete this process. Is there a better way to copy this information over in less time?
Here's the code that ports the info in:
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileName +
";Extended Properties='Excel 12.0 XML;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + sheetName + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
dtTemp.Reset();
dtTemp.TableName = userUpload;
sda.Fill(dtTemp); //Puts imported table into the dtTemp table
con.Close();
string fields = "";
string tempString;
foreach (DataColumn col in dtTemp.Columns) //Generates SQL String from imported table
{
tempString = RemoveSpecialCharacters(col.ColumnName);
if (dtTemp.Columns.IndexOf(col) == dtTemp.Columns.Count - 1)
{
fields += " [" + tempString + "] varchar(255)";
}
else
{
fields += " [" + tempString + "] varchar(255)" + ",";
}
}
fields = fields.Trim();
// createSQLConn(fields, connection, connectionTempDB, dtTemp);
}

While reading excel row value it ignores decimal values like 45.4 it read as only 45

while reading excel row value it ignores decimal values like 45.4 it read as only 45.
this is my code
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 8.0;HDR=NO;IMEX=2\";"); /*for office 2007 connection*/
conn.Open();
string strQuery = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataTable ExcelToDataTable = new System.Data.DataTable();
adapter.Fill(ExcelToDataTable);
I want to read as 45.4 not only 45. please help?

Read Data from Excel using OLEDB

I read an excel file using OLEDB. Below is the code:
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 SSQL = "SELECT * from [ Sheet1$ ]";
OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);
DataSet ds = new DataSet();
oleDA.Fill(ds);
DataTable _DtTable = ds.Tables[0];
oleDBCon.Close();
dataGridView1.DataSource = _DtTable;
foreach (DataRow rows in _DtTable.Rows)
{
string Description = rows[0].ToString();
string Code= rows[1].ToString();
textBox1.AppendText("Printing Description: " + Description + " and Code: " + Code + ",Date:" + DateTime.Now.ToString() + Environment.NewLine);
}
The excel file is as follows:
The data printed in textBox1 are:
Printing Description:Desc 2 and Code: Code 2,Date:20/12/2014 12:36:54 μμ
Printing Description: Desc 3 and Code: Code 3,Date:20/12/2014 12:36:54 μμ
So, my problem is that the 1st row of excel is going to the header of the Datatable. How can I avoid that (without adding any extra 1st row to excel)?
Just add "HDR=No" at the end of your connection string which means "No header row that indicates column but it contains data", then you will be able to fetch 1st row data also.
So your complete connection string would be
conn = ("Provider=Microsoft.ACE.OLEDB.12.0;" +
("Data Source=" + _filename + ";" +
"Extended Properties=\"Excel 12.0;\";HDR=No"));

C# Failing To Import All of the Cells from an Excel Spreadsheet

I am using some legacy code to return an Excel worksheet as a Dataset. However, when I iterate over the resulting data set it seems that not all of the cells are there. The Excel sheet that is being read has some merged cells and I am wondering if that is the problem. Here is the code:
private DataSet Get_Spreadsheet_Data(string strFileName, string strSheetName)
{
DataSet ds = new DataSet();
string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFileName + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(strConnectionString);
try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + strSheetName + "$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(ds);
}
catch (Exception Ex)
{
//litOutput.Text = "<span style=\"color:red;\">Exception Occurred pulling data from the spreadsheet.</span><br>Details: " + Ex.Message;
}
finally
{
objConn.Close();
objConn.Dispose();
}
return ds;
}
Is this code malfunctioning? Any advice is appreciated.
string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFileName + ";" + "Extended Properties=Excel 8.0;";
needed to read:
string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFileName + ";" + "Extended Properties="Excel 8.0;HDR=NO;IMEX=1;";
and that did the trick!
Have you tried running the same code with an Excel file that doesn't have the merged files?
That is the first thing I would try if I would wonder if merged cells can cause problems filling your dataset...
Edit for clarification:
For debugging purposes: use the same Excel file, only make sure you undo the merging of the cells.
Even better is to start with an excel file with 3 rows and 3 columns:
Row one: Cell A1 with value 'Foo; Cell B1 'Bar', C1 banana.
Row two: Cell A2 Foo1 B2 Bar1 <-- merge those two cells. C2 = Apple.
Row three: Cell A3 with value 'Foo2; Cell B3 'Bar2' C3 Orange <-- to check if the next line is read well after using merged cells...

Categories

Resources