C # oledb reads Excel files to avoid columns with scientific notation

Source: Internet
Author: User

 

In Excel, when you enter more than 11 digits, it is automatically converted into a scientific counting method, for example, 123456789012345. After the input, it becomes 1.23457e14, in excle, you can change the format of this column to text. Or Add "'", that is, '123. Enter a pair of single quotes (') and a numerical value. Therefore, Excel is regarded as a text instead of a scientific notation.

 

 

 

Use oledb of C # To import EXCEL to the database and generate scientific notation to solve the problem.

 

It is found that the records read by oledb for long numbers in the Excel column are displayed in scientific notation. 450107630382345 to 4.5010763038 + 14.

 

The main problem is the connection string.

"Provider = Microsoft. Ace. oledb.12.0; Data Source =" + filename + "; extended properties = 'excel 8.0; HDR = yes; IMEX = 1 '";

 

What does excel IMEX and HDR in oledb mean?

Extended properties = "" Excel 8.0; HDR = yes; IMEX = 1 ""

A: HDR (header row) settings

If the value is yes, the first row of the worksheet in the Excel file is the column name.

If the value is no, the first row of the worksheet in the Excel file is the item, and there is no column name.

B: IMEX (Import Export mode) settings

There are three modes for IMEX, And the read/write operations caused by these two modes are also different:

0 is export Mode

1 is import Mode

2 is linked mode (full capabilities)

Here I want to declare the IMEX parameter, because the incorrect mode represents an incorrect read/write operation:

When IMEX = 0, the "Export mode" is enabled. The Excel files opened in this mode can only be used for "writing.

When IMEX = 1, it is in import mode. The Excel files opened in this mode can only be used for reading.

When IMEX = 2, the connection mode is used. The Excel files enabled in this mode can support both read and write functions.

The so-called "4.5010763038 + 14" is only used for display, but is actually "450107630382345 ". Therefore, when IMEX = 1 is used for reading, "4.5010763038 + 14" is obtained ".

When IMEX is set to 0, the value "450107630382345" is returned ".

 

We need to change the connection string

"Provider = Microsoft. Ace. oledb.12.0; Data Source =" + filename + "; extended properties = 'excel 8.0; HDR = yes; IMEX = 0 '";

The problem is solved.

 

 

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.