我在C#.Net中将数据导出到Excel工作表.我有一个列有"00123450098"的数据.导出的数据没有第一个零.我想显示数据.
这是我的导出excel代码.
string style = @"<style> .text { mso-number-format:\@; } </style> ";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HtmlForm frm = new HtmlForm();
...................
...................
table.RenderControl(htw);
HttpContext.Current.Response.Write(style);
//render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
Run Code Online (Sandbox Code Playgroud)
小智 20
导出到excel时,\t在插入值之前添加将解决问题.例如:
string test = "000456";
string insertValueAs = "\t" + test;
Run Code Online (Sandbox Code Playgroud)
然后,字符串测试将被视为字符串值,而不是整数值.因此,它将保留前导零.
我遇到了同样的问题,上面的解决方案对我有用.希望这篇文章有帮助!
如果导出为CSV/TSV,请将其放入包含带有前导0或(特别是)16+位的文本"数字"的每个单元格中:
= "0012345"
..where 0012345是您要导出到该单元格的数字.
我希望我能记住我在哪里看到的.
在 Excel 文件中,数字单元格总是去除前导零,您可以通过跟随单引号来设置带有前导零的数字。IE
00123450098 到 '00123450098
但随后,该单元格的格式将更改为文本。如果您生成的 excel 文件有任何公式,其中包含该单元格引用作为数字,那么它将无法按预期工作。
| 归档时间: |
|
| 查看次数: |
19829 次 |
| 最近记录: |