如何使用子网格上的网格线将嵌套的gridview导出为ex​​cel / word

时间:2012-04-17 08:27:00

标签: c# asp.net visual-studio-2010

我尝试在不同的论坛上发帖提问,但仍然没有回复..任何人都可以帮助我吗?我已经尝试了大多数编码我收集出口嵌套gridview ..然而他们大多数给了我相同的输出..事实上我想要的是非常“简单”

码(EXCEL)

protected void Export(GridView gridView)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.xls"));
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

    //  Create a table to contain the grid
    Table table = new Table();

    //  include the gridline settings
    table.GridLines = gridView.GridLines;

    //  add the header row to the table
    if (gridView.HeaderRow != null)
    {
        PrepareControlForExport(gridView.HeaderRow);
        gridView.HeaderRow.Style.Add("background-color", "");
        table.Rows.Add(gridView.HeaderRow);
    }

    //  add each of the data rows to the table

    foreach (GridViewRow row in gridView.Rows)
    {
        PrepareControlForExport(row);
        table.Rows.Add(row);
    }

    //  add the footer row to the table
    if (gridView.FooterRow != null)
    {
        PrepareControlForExport(gridView.FooterRow);
        table.Rows.Add(gridView.FooterRow);
    }

    for (int i = 0; i <= gridView.Rows.Count; i++)
    {
        table.Rows[i].Cells[0].Visible = false;
    }

    using (StringWriter stringWriter = new StringWriter())
    {
        using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
        {
            //  render the table into the htmlwriter
            table.RenderControl(htmlWriter);

            //  render the htmlwriter into the response
            HttpContext.Current.Response.Write(stringWriter.ToString());
            HttpContext.Current.Response.End();
        }
    }
}

代码(字)

public static void Export(GridView gridView)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.doc"));
    HttpContext.Current.Response.ContentType = "application/ms-word";
    //  Create a table to contain the grid
    Table table = new Table();

    //  include the gridline settings
    table.GridLines = gridView.GridLines;

    //  add the header row to the table
    if (gridView.HeaderRow != null)
    {
        PrepareControlForExport(gridView.HeaderRow);
        table.Rows.Add(gridView.HeaderRow);
    }

    for (int j = 0; j < gridView.Columns.Count; j++)
    {
        //Apply style to Individual Cells
        gridView.HeaderRow.Cells[j].Style.Add("background-color", "black");
    }

    //  add each of the data rows to the table
    foreach (GridViewRow row in gridView.Rows)
    {
        PrepareControlForExport(row);
        table.Rows.Add(row);
    }

    //  add the footer row to the table
    if (gridView.FooterRow != null)
    {
        PrepareControlForExport(gridView.FooterRow);
        table.Rows.Add(gridView.FooterRow);
    }

    for (int i = 0; i <= gridView.Rows.Count; i++)
    {
        table.Rows[i].Cells[0].Visible = false;
    }

    using (StringWriter stringWriter = new StringWriter())
    {
        using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
        {
            //  render the table into the htmlwriter
            table.RenderControl(htmlWriter);

            //  render the htmlwriter into the response
            HttpContext.Current.Response.Write(stringWriter.ToString());
            HttpContext.Current.Response.End();
        }
    }
}

下面的图片是从excel ...

导出的

exported to excel

下面的图片是从word ...

导出的

exported to word

1 个答案:

答案 0 :(得分:0)

经过这么多天的测试后,我终于找到了解决方案:将CellSpacing="2"添加到gridview设计中,这就是全部。