asp.net how do I export excel/csv text format data?

Source: Internet
Author: User
Tags foreach mixed tostring

Just started to do Excel related projects, so the problems encountered regardless of size are recorded

By chance, when you add data, you change it all to numbers. Results when the output is fully automatic into the scientific notation, this is Excel's powerful function, can automatically identify numbers and strings, too clever but some trouble, like if the ID card (18 digits), it is not. Over 11, here's a look at some of the data to summarize the solution:

Method 1: When adding data to Excel, add single quotes to the front of the data, which is my solution.

such as: Sheet.cells[irow, icol] = "'" + ds. Tables[0]. Rows[irow-3][itm.id]. ToString ();

Method 2: Add style to Style::

The code is as follows:

foreach (DataRow row in TB. Rows) {data = ""; foreach (DataColumn column in TB). Columns) {if (column). Columnname.equals ("voucher number") | | Column. Columnname.equals ("registration number")) Data + = "" + Row[column]. ToString () + ""; Elsedata + = "" + Row[column]. ToString () + "";}

Method 3:

Ado. NET reads Excel, it is based on the first n rows of data in the column (n counts from the registry hkey_local_machine/software/microsoft/jet/4.0/engines/excel/typeguessrows), The type of the column is judged based on the contents of the rows of data, which is independent of the type of the column in Excel (the text type should be preceded by a '). If the data in this n row has a number, which is a mixed type, the value is based on Hkey_local_machine/software/microsoft/jet/4.0/engines/excel/importmixedtype. If the Importmixedtype value is text, the column is literal, and if the value is majority type, the data is more than the type.

In order to read the columns with a number of text correctly, we need to set the Importmixedtype entry in the registry to Text,typeguessrows set to 0 (means to read all the data to determine if it is a mixed type)

Also note that the value of the IMEX will affect whether to use the Importmixedtype and MaxScanRows in the registry, if it is imex=1, and if 0 or 2 is not used. Imex=1 is an open, read-only connection, so read-only can be used in a read-only manner.

Related Article

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.