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