When querying Excel Data, column data is empty

Source: Internet
Author: User

When querying Excel data, if the values in a column contain numbers and texts, some text values may not be read from the data obtained using the query statement. for this reason, I want to find a large segment on the Internet.

In fact, it is very easy to solve this problem, as long as the connection string instructs all columns to be processed by text, so that all the data can be obtained.

String sconnectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + httpcontext. Current. server. mappath ("~ /App_data/database accessories example Table .xls ") + "; Extended properties = 'excel 8.0; IMEX = 1; HDR = 1 ;'";
Oledbconnection objconn = new oledbconnection (sconnectionstring );
Objconn. open ();
Oledbcommand objcmdselect = new oledbcommand ("select * from [sheet1 $]", objconn );
Oledbdataadapter objadapter1 = new oledbdataadapter ();
Objadapter1.selectcommand = objcmdselect;
Dataset objdataset1 = new dataset ();

Objadapter1.fill (objdataset1, "xldata ");
Objconn. Close ();
Objconn. Dispose ();
Dt = objdataset1.tables [0];

 

The key is the property setting in red.IMEXIndicates whether to forcibly convert to text

 HDR has no field. Generally, the column title of row 1st in the Excel table is the field name by default, such as name and age.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.