导出到excel时将数字转换为文本

Roh*_*han 2 c# excel

我在网格视图中显示我的数据(逗号分隔数字),并根据需要发生.但是,当我将其导出为ex​​cel时,则在显示方面更改该值

例如我的值是901155465,978785496,987458986然后它显示为901,155,465,978,785,496,987,458,986

这就是我将数据集传递给excel的方法.我知道我们也可以渲染HTML,但我只需要传输数据.


GridView GridView1 = new GridView();
GridView1.DataSource = myDataSet;
GridView1.DataBind();
string style = @" .text { mso-number-format:\@; }  "; 
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
esponse.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter s_Write = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter h_write = new HtmlTextWriter(s_Write);
GridView1.ShowHeader = true;
GridView1.RenderControl(h_write);
Response.Write(style); 
Response.Write(s_Write.ToString());
Response.End();

似乎excel将数字视为一个数字并在适当的位置添加逗号.

是否有任何显示数据的解决方案,如gridview所示.

提前致谢

Euc*_*bwè 9

试试这个:

_worksheet.Cells[1, 1] = "=\"" + YOUR_VALUE + "\"";
Run Code Online (Sandbox Code Playgroud)

这就是我使用Interop.Excel的方式

Excel将忽略(=),因为它启动一个公式,双引号将告诉excel将该值用作String.


Tim*_*ter 8

您也可以尝试导出excel样式表:

mso-number-format:"0"   NO Decimals
Run Code Online (Sandbox Code Playgroud)

http://cosicimiento.blogspot.fr/2008/11/styling-excel-cells-with-mso-number.html

因此你需要把它写到Response:

// ...
string style = @"<style> td { mso-number-format:"0"; } </style> ";
// Style is added dynamically
Response.Write(style);
Response.Write(s_Write.ToString());
// ...
Run Code Online (Sandbox Code Playgroud)