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 '";