How to read empty lines when reading from Excel

I use OLEDB to connect and read data from an Excel spreadsheet. I have IMEX = "1" and everything works fine. My problem is that the sheets that I am reading may start with a few blank lines, and the number of blank lines is important. For example, if I read a 5x5 grid, for example:

- - - - -
- - - - -
2 - 3 3 8
- - - - -
- - 5 2 2

where '-' is an empty cell. It is important that the first two lines are empty. The mesh size is dynamic. My code seems to ignore the first empty lines. But deals with an empty line in line 4 in order.

How to count the number of blank lines at the beginning of an Excel worksheet using OLEDB?

I will limit myself to using OLEDB, I would not want it if I did not need it; -)

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

".xls" .

+5
3

, . , :

     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]);
         }
     }

:

    string cnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Untitled 1.xls\";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
+1

: .

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();
+1

@Knvn

.xls .

0

All Articles