【问题标题】:How to Export multiple header Gridview Data to Excel如何将多个标题 Gridview 数据导出到 Excel
【发布时间】:2017-07-04 03:55:11
【问题描述】:

我有一个 gridview,它在运行时添加了多个标题,如下图所示:

我想将这个gridview中的数据导出到excel中,我有以下代码:

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gdView.HeaderRow.BackColor = Color.White;
            foreach (TableCell hcell in gdView.HeaderRow.Cells)
            {
                hcell.BackColor = Color.White;
            }
            foreach (GridViewRow row in gdView.Rows)
            {
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        cell.CssClass = "textmode";
                    }
                }
            }
            gdView.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
        // controller   
    }

我得到的excel输出如下:

如您所见,标题行的颜色继续超出表格区域。顶部的标题行也在 excel 输出中折叠。

请大家提出如何解决此问题的建议。

谢谢。

【问题讨论】:

  • 开始使用专门的库来创建 Excel 文件,例如 EPPlus。您现在所做的只是创建一个扩展名为 .xls 的 HTML 页面。

标签: c# asp.net excel


【解决方案1】:

我终于可以通过修改代码来解决这个问题了:

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            for (int x = 0; x <= 3; x++)
            {
                GridViewRow rows = (GridViewRow)gdView.HeaderRow.Parent.Controls[x];
                rows.BackColor = Color.White;
                rows.Height = 15;
                for (int i = 0; i <= rows.Cells.Count - 1; i++)
                {
                    rows.Cells[i].BackColor = Color.Maroon;
                }
            } 
            foreach (GridViewRow row in gdView.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    cell.VerticalAlign = VerticalAlign.Middle;
                    cell.CssClass = "textmode";
                }
            }
            gdView.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
        // controller   
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多