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

C# 如何在导出前格式化GridView中的列以在excel中显示所有数字?,c#,gridview,C#,Gridview,我正在尝试将GridView导出到Excel,我有一列包含一系列数字,如1245333325364。当我运行GridView查询时,我可以看到完整的数字,但当我导出到excel时,我看到的是该列上的1.00133E+12。我知道我可以让用户在excel中更改,但并不是所有文件都在导出后打开,他们只是将其直接保存到一个目录中。我真的希望在导出过程中更改列的格式,而不是让用户在保存文件之前进行更改。我正在用C#执行导出操作,任何帮助都将不胜感激 我用于导出GridView的代码如下所示: p

我正在尝试将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();
    }
protectedvoid exporttoexcel\u单击(对象发送方,事件参数e)
{
字符串日期=DateTime.Now.ToString(“MM dd yyyy”);
准备GridViewForExport(GridView1);
Response.Clear();
Response.Buffer=true;
Response.AddHeader(“内容处置”、“附件;文件名=“+date+”\u“+CHROUT.Text+”\u Trailer\u“+Trailer.Text”);
Response.Charset=“”;
Response.ContentType=“application/vnd.ms excel”;
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
GridView1.AllowPaging=false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add(“背景色”和“#FFFFFF”);
GridView1.HeaderRow.Cells[0].Style.Add(“宽度”,“105px”);
GridView1.HeaderRow.Cells[0].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[1].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[2].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[3].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[4].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[5].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[6].Style.Add(“背景色”,“CCCC”);
GridView1.HeaderRow.Cells[7].Style.Add(“背景色”,“CCCC”);
对于(int i=0;i
问题不在于单元格的宽度而不是格式吗?更改单元格的宽度,数字应该显示得很好。

问题不在于单元格的宽度而不是格式吗?更改单元格的宽度,数字应该显示得很好。

您可以将其
单元格格式设置为
数字
货币
替换其原始格式。

您可以将其
单元格格式
设置为
数字
货币
替换其原始格式。

尝试使用单个引号('))符号就在大数字之前。。。Excel将单引号识别为文本

或者,您可以尝试用双引号将值括起来,如“12345678901344”


希望这能奏效。

试着在大数字前面加一个引号(')符号。。。Excel将单引号识别为文本

或者,您可以尝试用双引号将值括起来,如“12345678901344”


希望这能奏效。

我终于找到了这个问题的答案。我基本上只需要在导出之前将格式添加到GridView,并在数据绑定上更加具体。请看下面的代码:

首先为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,并在数据绑定上更加具体。请看下面的代码:

首先为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">
就这些。

像这样试试

   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
            }
}
protectedvoid btnExportToExcel\u单击(对象、事件参数)
{
GridView gvExportExcel=新建GridView();
gvExportExcel.ID=“ExportExcel”;
gvExportExcel.AllowPaging=false;
gvExportExcel.DataSource=listOfData(通用列表);
gvExportExcel.DataBind();
对于(int i=0;i