我正在尝试将GridView导出到Excel,我有一个列有一系列数字的列,如1245333325364.当我运行GridView的查询时,我可以看到完整的数字,但是当我导出到excel时,我看到的是1.00133E +该专栏12.我知道我可以让用户在excel中更改此内容,但导出后并非所有文件都打开,只是将其直接保存到目录中.我真的想在导出过程中更改列的格式,而不是让用户在保存文件之前执行此操作.我在C#中执行导出任何帮助都会非常感激.
我用来导出GridView的代码是这样的:
protected void exporttoexcel_Click(object sender, EventArgs e)
{
string date = DateTime.Now.ToString("MM-dd-yyyy");
PrepareGridViewForExport(GridView1);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + date + "_" + CHROUT.Text + "_Trailer_" + TRAILER.Text);
Response.Charset = "''";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
GridView1.HeaderRow.Cells[0].Style.Add("width", "105px");
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[4].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[5].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[6].Style.Add("background-color", "#CCCCCC");
GridView1.HeaderRow.Cells[7].Style.Add("background-color", "#CCCCCC");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "texmode");
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#f0f0f0");
row.Cells[1].Style.Add("background-color", "#f0f0f0");
row.Cells[2].Style.Add("background-color", "#f0f0f0");
row.Cells[3].Style.Add("background-color", "#f0f0f0");
row.Cells[4].Style.Add("background-color", "#f0f0f0");
row.Cells[5].Style.Add("background-color", "#f0f0f0");
row.Cells[6].Style.Add("background-color", "#f0f0f0");
row.Cells[7].Style.Add("background-color", "#f0f0f0");
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .text { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
我终于得到了这个问题的答案.我基本上只需要在导出之前将格式添加到GridView,并在DataBound上更具体.看看下面的代码:
首先为OnRowDataBound创建一个事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
e.Row.Cells[2].Attributes.Add("class", "text");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在GridView上引用它,如下所示:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
Run Code Online (Sandbox Code Playgroud)
然后在导出GridView之前添加这一小段代码.
Response.Write(style);
Run Code Online (Sandbox Code Playgroud)
就这样.
| 归档时间: |
|
| 查看次数: |
13964 次 |
| 最近记录: |