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

本文关键字:excel 数字 显示 GridView 格式化 | 更新日期: 2023-09-27 17:55:35

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

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

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

仅此而已。

尝试这样做

   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
            }
}

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

尝试在

大数字之前放置一个单引号 (') 符号... Excel 将单引号识别为文本。

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

希望这有效。

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