繁体   English   中英

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

[英]How can I format a column from a GridView before export to show all numbers in excel?

我正在尝试将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中,并且在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
            }
}

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

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

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

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

希望这行得通。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM