Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count empty rows when reading from Excel

Tags:

c#

excel

oledb

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.

like image 991
RandomDev Avatar asked Nov 04 '22 15:11

RandomDev


1 Answers

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\"";
like image 122
NaveenBhat Avatar answered Nov 09 '22 10:11

NaveenBhat