Questions about reading Excel Data to the gridview (to be improved)

Source: Internet
Author: User

References

Http://www.knowsky.com/345281.html

(1) Data Reading
The data in the Excel table is as follows:
A B C
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3
4 A4 B4 C4
5 A5 B5 C5

Select * from [sheet1 $]
The data read is as follows:
A2 B2 C2
A3 B3 C3
A4 B4 C4
A5 B5 C5

Select * from [sheet1 $ A1: C5]
The data read is as follows:
A2 B2 C2
A3 B3 C3
A4 B4 C4
A5 B5 C5

Select * from [sheet1 $ A3: C5]
The data read is as follows:
A4 B4 C4
A5 B5 C5

The problem arises: the first row in the valid region cannot be read.
Cause: Based on the default connection string, the data provider uses the first row in the valid region as the column name (specifically, F1, F2, F3, or others are displayed, which is to be studied, currently, only Chinese characters are displayed)
Solution: Modify the connection string
Default connection string
"Provider = Microsoft. Jet. oledb.4.0; Data Source =" + @ "G: \ test excel.xls" + "; extended properties = 'excel 8.0 ;'"
After modification
"Provider = Microsoft. Jet. oledb.4.0;" + "Data Source =" + @ "G: \ test excel.xls" + "; extended properties = 'excel 8.0; HDR = no ;'"
Explanations:
HDR = No indicates that the first row in the valid region is used as the data.
HDR = Yes indicates that the first row in the valid region is used as the column name.

Problem 2: The data displayed in the Excel table is different in the gridview.
Cause: when reading a file, Excel takes the Data Type of the first row as a reference.
Solution: Modify the connection string
Default connection string
"Provider = Microsoft. Jet. oledb.4.0; Data Source =" + @ "G: \ test excel.xls" + "; extended properties = 'excel 8.0 ;'"
After modification
"Provider = Microsoft. Jet. oledb.4.0;" + "Data Source =" + @ "G: \ test excel.xls" + "; extended properties = 'excel 8.0; IMEX = 1 ;'"
Explanations:
IMEX = 1 to read hybrid data as text type

Summary:
Integrated Question 1 and Question 2
The connection string is written as follows:
"Provider = Microsoft. jet. oledb.4.0; "+" Data Source = "+ @" G: \ test excel.xls "+"; extended properties = 'excel 8.0; HDR = no; IMEX = 1 '";

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.