HTML Export to Excel data format incorrect workaround

Source: Internet
Author: User

Colleague quit, the hand of the program to me, the next day the customer said: "Why do we export Excel does not summarize it?" ”

。。。。 You wait a moment, I look at ha (how do I know why Excel does not summarize your export!!) )

Open Program

No problem, then export open

This is God horse? All that is in the text format. Text format stored numbers how to summarize what you told me.

Look at the code.

1 voidExportexcel ()2     {3DataTable dt =getdatatable ();4         if(dt! =NULL)5         {6 response.clear ();7Response.Buffer =true;8Response.Charset ="GB2312";9Response.appendheader ("content-disposition","attachment;filename="+ System.Web.HttpUtility.UrlEncode (exceltitle. Value, System.Text.Encoding.UTF8) +". xls");TenResponse.ContentEncoding =System.Text.Encoding.UTF8; One  AResponse.ContentType ="Application/ms-excel"; -              This. EnableViewState =false; -System.IO.StringWriter Ostringwriter =NewSystem.IO.StringWriter (); theSystem.Web.UI.HtmlTextWriter Ohtmltextwriter =NewSystem.Web.UI.HtmlTextWriter (ostringwriter); -  -Gvexport =NewSystem.Web.UI.WebControls.GridView (); -Gvexport.datasource =dt. DefaultView; +Gvexport.allowpaging =false; - Gvexport.databind (); +GVEXPORT.ATTRIBUTES.ADD ("style","vnd.ms-excel.numberformat:@"); A             //Back to client at Gvexport.rendercontrol (ohtmltextwriter); - Response.Write (Ostringwriter.tostring ()); - Response.End (); -  -   -         } in close (); -}
View Code

Data format for Excel: GvExport.Attributes.Add ("style", "vnd.ms-excel.numberformat:@");

Here are some of the commonly used 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.0%

The format you output here is Text! Decisive change to the third number type (haha, I am not very smart)

Wait, my table head of the date, also become a number, see it has two decimal places on the scratching head (mistakes, mistakes ha)

GVEXPORT.HEADERROW.ATTRIBUTES.ADD ("Style", "vnd.ms-excel.numberformat:@");

gvExport.Attributes.Add ("style", "vnd.ms-excel.numberformat:#,# #0. xx");Change the table head to the original on the line, we do not change to the time type. Ok!  Perfect, perfect!!! This article by @ Paris _ handsome original, without consent may not be reproduced

HTML Export to Excel data format incorrect workaround

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.