如何在导出前从GridView格式化列以显示excel中的所有数字?

时间:2011-05-05 23:14:36

标签: c# gridview

我正在尝试将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();
    }

5 个答案:

答案 0 :(得分:5)

我终于得到了这个问题的答案。我基本上只需要在导出之前将格式添加到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");
    }
}

然后在GridView上引用它,如下所示:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

然后在导出GridView之前添加这一小段代码。

Response.Write(style);

就是这样。

答案 1 :(得分:2)

试试这个

   protected void btnExportToExcel_Click(object s, EventArgs e)
    {

    GridView gvExportExcel = new GridView();
                        gvExportExcel.ID = "ExportExcel";
                        gvExportExcel.AllowPaging = false;
                        gvExportExcel.DataSource = listOfData(Generic list);
                        gvExportExcel.DataBind();
                        for (int i = 0; i < gvExportExcel.Rows.Count; i++)
     gvExportExcel.Rows[i].Cells[0].Attributes.Add("style", "mso-number-format:\\@");

                        Export("Test.xls", gvExportExcel);
    }


private void Export(string fileName, GridView dgvExport)
        {
            try
            {
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                Response.Charset = string.Empty;
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                dgvExport.RenderControl(htw);

                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.Flush();
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                HttpContext.Current.Response.Close();
                //HttpContext.Current.Response.End();

            }
            catch (System.Threading.ThreadAbortException ex)
            {
                //write your exception
            }
}

答案 2 :(得分:0)

问题不是单元格宽度而不是格式吗?更改单元格的宽度,数字应显示正常。

答案 3 :(得分:0)

您可以将其CellFormat设置为NumberCurrency替换其原始格式。

答案 4 :(得分:0)

尝试在大号之前加上单引号(')符号 ... Excel将单引号识别为文本。

或者您可以尝试将值括在双引号中,如“12345678901234”

希望这有效。