C# read excel file problem

Hi, experts

I try to read excel files by the following code. I would like to know number or rows, and number of columns of the excel sheet.

In some excel sheet, I do found it give me more number of columns than it is shown in the original excel files. I google it and I guess it could be "hidden column"??

Is there a way to eliminate those empty hidden column (actually I just need the to know the correct number of column)

thanks in advance
OleDbConnection cn_xls = new OleDbConnection();
string str_conn_xls = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\VM_SSIS\\xls_In\\1907_PHR_V22_I42.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";

cn_xls.ConnectionString = str_conn_xls;

 // for Data Adapter
string sSQL = "SELECT * FROM  [Sheet10$]";
OleDbDataAdapter objCmd_xls = new OleDbDataAdapter(sSQL, cn_xls);

// for Data Set
DataSet objDS = new DataSet();
objCmd_xls.Fill(objDS, "tmp_table1");
DataTable DT_xls = objDS.Tables[0];

int the_Rsize = DT_xls.Rows.Count;
int the_Csize = DT_xls.Columns.Count;
MessageBox.Show("num of row " + the_Rsize.ToString());
MessageBox.Show("num of col " + the_Csize.ToString());

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can directly get this by excel automation. You need not to use it as a datasource. Also, Jet provider is not supported on 64 bits (to the latest of what I know).

Check out:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft KB on excel automation:

Hope it helps.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.