我有下面的代码,单击时会生成一个excel文件。代码运行正常,颜色的格式很好。网格视图的数据前导零没有显示在excel中,长数字“106638660952840428”显示为1.06639E + 17,值为106638660952840000,因此它也会丢失其最后四位数字。我尝试了几种方法来导出具有相同结果的所有方法。我这样做是为了有机会单独格式化单元格。所以问题是有没有人知道如何动态更改格式,以便不会出现这些问题?另外要添加的是网页浏览器中的网格视图确实正确显示数据。
protected void LinkButton6_Click(object sender, EventArgs e)
{
Iframe1.Attributes.Add("src", "blank.aspx");
if (CheckBox5.Checked || CheckBox6.Checked)
{
dt = (DataTable)Cache["dtable"];
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=BemisInventory.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
//GridView1.PageIndex = 0;
GridView1.DataBind();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = Color.White;//GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = Color.White;
}
else
{
cell.BackColor = Color.White; //GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
}
答案 0 :(得分:-1)
通过添加:string style = @&#34; .textmode {mso-number-format:\ @; }&#34 ;;提出警告,但所有数据都在那里。