How does Asp.net export data in Excel/Csv text format?
I just started to work on Excel-related projects, so I should record all the problems regardless of the size.
By chance, when adding data, all the data is converted into numbers, and the result output is automatically converted into scientific notation. This is a powerful excel function that can automatically recognize numbers and strings, it's too clever, but it's a little troublesome, just like if you enter your ID card (18 digits. After more than 11 digits, I checked some information and summarized the solution:
Method 1: When adding data to excel, add single quotation marks before the data. My solution is this method.
For example, Sheet. Cells [iRow, iCol] = "'" + ds. Tables [0]. Rows [iRow-3] [itm. ID]. ToString ();
Method 2: Add a style to the style ::
The Code is as follows:
Foreach (DataRow row in tb. rows) {data + = ""; foreach (DataColumn column in tb. columns) {if (column. columnName. equals ("ID number") | column. columnName. equals ("Registration No.") data + = "" + row [column]. toString () + ""; elsedata + = "" + row [column]. toString () + "";}
Method 3:
ADO. NET when reading Excel, it will be based on the first n rows of data in the column (n numbers from the Registry HKEY_LOCAL_MACHINE/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows ), determine the type of the column based on the data of these rows. This type is irrelevant to the type of the column in Excel (the text type should be a 'before the data '). If the data in the n rows contains numbers in the text, that is, the hybrid type, the value is determined by HKEY_LOCAL_MACHINE/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedType. If the ImportMixedType value is Text, the column is Text. If the value is Majority Type, multiple data types are used.
In order to read the columns with numbers and texts correctly, we need to set ImportMixedType in the Registry to text and TypeGuessRows to 0 (indicating that all data needs to be read before determining whether the data type is mixed)
In addition, the IMEX value affects whether to use ImportMixedType and MAXSCANROWS in the registry. If IMEX is set to 1, it is used. If it is set to 0 or 2, it is not used. IMEX = 1 is a read-only connection opened. Therefore, to read the connection correctly, you can only use the read-only method.