System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView gv = new GridView();
gv.EnableViewState = false;
gv.DataSource = tbl;
gv.DataBind();
gv.RenderControl(hw);
string style = @"<style type=""text/css"">td{mso-number-format:'\@';}</style>";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+DateTime.Now.ToFileTime()+".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
Response.Output.Write(style);
Response.Write(tw.ToString());
Response.Flush();
Response.End();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
GridView gv = new GridView();
gv.EnableViewState = false;
gv.DataSource = tbl;
gv.DataBind();
gv.RenderControl(hw);
string style = @"<style type=""text/css"">td{mso-number-format:'\@';}</style>";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+DateTime.Now.ToFileTime()+".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
Response.Output.Write(style);
Response.Write(tw.ToString());
Response.Flush();
Response.End();
本文介绍了一个C#解决方案,用于将GridView数据导出到Excel时确保全数字列保持其前导0。通过设置`mso-number-format`样式为`'@'`,可以防止数字格式化导致的0被自动去掉。
2042

被折叠的 条评论
为什么被折叠?



