0

I want to get the number of rows that are non-empty in an excel sheet. I am using asp.net and C#, and in my form I am uploading a simple excel file, with the number and names of column is decided by the user only (means the data column names are unknown). I am looking for a perfect way to get the number of excel rows that are not empty. The following code I am using gives a count of 78 for a list of rows having only 3 rows of non empty data (excluding the first row, as it is used for column name). Here is my code:

con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathExcel + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
string cmdS = "SELECT * FROM [Sheet1$]";
if (con.State == ConnectionState.Closed)
con.Open();
OleDbCommand cmd = new OleDbCommand(cmdS, con);
OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "MailingList");
DataRow dr;
dr = ds.Tables["MailingList"].Rows[0];
int intExcelRowCount=0;
// the following thing give me output as 78 but my sheet has only 3 records.
intExcelRowCount=int.Parse(ds.Tables["MailingList"].Rows.Count);

Any perfect way to do this?

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Cyberpks
  • 1,401
  • 6
  • 21
  • 51
  • Did you see this? http://stackoverflow.com/questions/5025983/how-to-upload-only-non-empty-rows-of-excel-spreadsheet-using-oledb-in-c – Siddharth Rout Feb 19 '13 at 07:39
  • Thanks for that, but I've mentioned that the column names are not predefined. So cant use that Query. – Cyberpks Feb 19 '13 at 07:45
  • I **highly** recomment using a third party Excel library instead of `OleDb`. See [here](http://stackoverflow.com/a/9155907/1174942). – Mohammad Dehghan Feb 19 '13 at 08:03

0 Answers0