如何将多个标题gridview数据导出到excel [英] How to export multiple header gridview data to excel

查看:95
本文介绍了如何将多个标题gridview数据导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,它在运行时添加了多个标题。我想将gridview数据导出为具有相同格式的excel。但是当我尝试这样做时,多个标题行的颜色继续超出输出excel文件中的表区域,这是不希望的。用于导出数据的代码如下。请帮忙解决这个问题。

谢谢。



我尝试了什么:



< code> 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(gCView.HeaderRow.Cells中的TableCell hcell)
{
hcell.BackColor = Color.White;
}
foreach(gdView.Rows中的GridViewRow行)
{
{
row.BackColor = Color.White;
foreach(TableCell cell in row.Cells)
{
cell.CssClass =textmode;
}
}
}
gdView.RenderControl(hw);
//格式将数字格式化为字符串
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
}< / code>

解决方案

我可以通过修改以下代码来解决问题:



 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(gdView.Rows中的GridViewRow行)
{
row.BackColor = Color.White;
foreach(TableCell cell in row.Cells)
{
cell.VerticalAlign = VerticalAlign.Middle;
cell.CssClass =textmode;
}
}
gdView.RenderControl(hw);
//格式将数字格式化为字符串
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
}


I have a gridview which has multiple headers added during run time. I want to export the gridview data to excel with the same formating. But while I try to do so the color of the multiple header rows continues beyond the table area in output excel file, which is not desired. The code used for exporting data is as follows. Please help to solve this.
Thanks.

What I have tried:

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

解决方案

I could finally solve the issue by modifying the code as below:

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   
        }


这篇关于如何将多个标题gridview数据导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆