0

我已经尝试在不同的论坛上发布问题,但仍然没有回复.. 有人真的可以帮助我吗?我已经尝试了我在导出嵌套网格视图时收集的大多数编码。但是它们中的大多数给了我相同的输出。实际上我想要的是非常“简单”

代码(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导出的...

导出到excel

下面的图片是从word中导出的...

导出到word

4

1 回答 1

0

omg 经过这么多天的测试,我终于找到了解决方案:添加CellSpacing="2"到 gridview 设计中,仅此而已。

于 2012-04-18T06:55:06.557 回答