简体   繁体   中英

DataTable Row Count Returning 2 when only one VB.NET

I upload an excel file with only one row and one column or an empty excel file. I put this data into a datatable. I then do datatable.rows.count and it always gives me 2. Unless there are more than two records, then it gives an accurate count. Help?

(Presuming that you may actually have a header in your Excel document) This could be because it is counting your header as a row. In your connection string please specify HDR in Extended Properties like so:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\test.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";

More information here: http://www.connectionstrings.com/excel/

If you are using ODBC as opposed to OleDB to connect then your connection string and drivers will be slightly different: http://www.connectionstrings.com/microsoft-excel-odbc-driver/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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