. Net control data imported to Excel format
First, let's take a look at how Excel exports from a Web page. When we send the data to the client, we want the Client Program (browser) to read it in Excel format, so
Set the MIME type to application/vnd. MS-Excel. Data is displayed in the format of each cell when the Excel file is read.
Then, Excel displays the data of the cell in the default format. This provides us with a space for custom data formats. Of course, we must use the formats supported by Excel. Below
Common formats:
1) Text: VND. ms-excel.numberformat :@
2) Date: VND. ms-excel.numberformat: yyyy/mm/dd
3) number: VND. ms-excel.numberformat: #,## 0.00
4) Currency: VND. ms-excel.numberformat: ¥ #, #0.00
5) percentage: VND. ms-excel.numberformat: # 0.00%
You can also customize these formats. For example, you can define the year and month as YY-mm. So how can I add these formats to cells when I know these formats? It's easy. We just need to add the style
Add the corresponding tag pair (closed tag. For example, <TD> </TD>, add a style to the tag pair <TD> </TD>, as shown in
Below: <TD
Style = "Vnd. ms-excel.numberformat: @"> 410522198402161833 </TD>
Similarly, you can add a style to <div> </div> or <tr> <
/Tr>, <Table> </table> to add a style. When we add a style to both the parent and child tags, the data is displayed in a style.
What about it? After testing, it is displayed in the style closest to the data.
In Asp.net, The DataGrid data is exported to excel to prevent the numeric character format from becoming scientific count.
Write in itemdatabound event
'Modi by mingfei 20070712 prevents the ID card number from changing to a scientific count cell (4) as a numeric number when it is exported
If (E. Item. itemtype = listitemtype. item or E. Item. itemtype = listitemtype. alternatingitem) then
E. Item. cells (4). Attributes. Add ("style", "Vnd. ms-excel.numberformat :@")
End if