从网格导出excel时的前导零问题

时间:2015-04-18 09:55:08

标签: c# asp.net export-to-excel

在下面的代码中,我导出excel从网格导出时,前导零被删除。我想保持前导零。请帮我解决问题。

HtmlForm form = new HtmlForm();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Supply/DemandReport.xls"));
Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gdSupplyorDemand.AllowPaging = false;
gdSupplyorDemand.HeaderStyle.ForeColor = System.Drawing.Color.Black;

form.Attributes["runat"] = "server";
form.Controls.Add(gdSupplyorDemand);
this.Controls.Add(form);
form.RenderControl(hw);

string style = @"<style> .textmode { mso-number-form at:\@;}</style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

2 个答案:

答案 0 :(得分:0)

在Excel中,数字单元格总是剥离前导零。一个技巧是使其成为字符串而不是数字。您可以使用"\t"

例如: -

     int Value=00123  
     string newValue="\t"+value;

这应该有用......

答案 1 :(得分:0)

像@kyle说的那样,添加\ t。但首先声明一个字符串并将string的值设置为sw.ToString()。这将解决您的问题。祝你好运。