简体   繁体   中英

How can I compare Excel files? OleDbDataAdapter doesn't read string column in Excel tab?

Our Excel 2013 xlsx file has tab "DEPTS" and this tab has a column called "1F/3F". Each cell in this column can have one of these values: "5", "Ati_3", "4", "Btu_4", etc.

Before today, I would move the contents of this tab to a dataset with this straightforward snippet. The dataset viewer would display all rows and all columns:

string connectionString = string.Format(ExcelConnstring, FileName);
string deptsSql = string.Format("SELECT * FROM [{0}$]", "DEPTS");

DataSet deptsDataset = new DataSet();
using (OleDbConnection con = new OleDbConnection(connectionString))
{
    con.Open();
    OleDbDataAdapter adapter = new OleDbDataAdapter(deptsSql, con);
    adapter.Fill(deptsDataset);
    con.Close();
}
return deptsDataset;

Today, I try to upload today's file, which is the same exact format. When I look at the contents of the dataset, I notice that the cells in column "1F/3F" that are not numerical are empty. It's reading all 40 rows, but those particular cells whose values could be "Ati_3", "Btu_4" (ie. not numeric) are being read as empty. The numeric values are being read correctly.

How can I compare an older file with this file? The file seems to be correct, and I have no idea how to check if something was added to that particular column that would cause the error.

Thanks.

The ADO.NET driver uses the data in each column (the first 10 rows or so) to determine its datatype, which is terrible but it is what it is. If you have a column which has numeric values in the first 10 or so rows, the driver treats that as a numeric column and will read any non-numeric values as null.

Cell formats in the excel document are not honored by the driver. If you want to ensure that the data is read as text, and you have control over the process that generates the excel document, you can force the column to be treated as text by inserting 10 or so dummy values (eg 'Ignore') and throw away those rows after you have read the contents.

By ensuring that the first 10 rows of a column contain text, the driver will correctly read the numeric and non-numeric values for that column (they will all be treated as text).

If you cannot control the creation of the file you are going to read you could switch to another technology to read the Excel document. Some alternatives include:

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