简体   繁体   English

从Excel读取时如何计算空行

[英]How to count empty rows when reading from Excel

I'm using OLEDB to connect and read through data from an Excel spreadsheet. 我正在使用OLEDB来连接和读取Excel电子表格中的数据。 I have IMEX="1" and everything works ok. 我有IMEX =“1”,一切正常。 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: 例如,如果我正在读取5x5网格,如:

- - - - -
- - - - -
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. 但处理第4行的空行确定。

How can I count the number of empty rows at the start of an Excel sheet using OLEDB? 如何使用OLEDB计算Excel工作表开头的空行数?

I'm restricted to using OLEDB, I wouldn't if I didn't have to ;-) 我被限制使用OLEDB,如果我没有,我就不会;-)

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 UPDATE

Specifying '.xls' as the file extension in the connection string fixed this issue and correctly reads the empty rows at the start. 在连接字符串中指定'.xls'作为文件扩展名修复了此问题,并在开始时正确读取空行。

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

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\"";

As stated by @Knvn 如@Knvn所述

You need to specifiy the file extension .xls with the file name in your connection string. 您需要在连接字符串中使用文件名指定文件扩展名.xls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM