解决思路:在Excel中作一个包含有"012457890"的内容,设定单元格的显示方式,然后保存成Html的文件,在查看源代码.
发现在CSS格式定义中有:td{mso-number-format:"\@";}.这样问题就容易解决了.
实际伤得代码只是比常见的处理方式多了一句而已.
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=WebOrderExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvOrder.Page.EnableViewState = false;
tw.WriteLine(strStyle);
gvOrder.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=WebOrderExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvOrder.Page.EnableViewState = false;
tw.WriteLine(strStyle);
gvOrder.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();