【问题标题】:Export to excel returning blank excel spreadsheet导出到 excel 返回空白的 excel 电子表格
【发布时间】:2016-06-22 17:43:38
【问题描述】:

我有以下代码可以将GridView 导出到 excel 文件中。从理论上讲,此代码应该可以轻松导出。但是,它正在导出空白。

public static void Export(string fileName, GridView gv, System.Web.UI.Control parentControl)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=" + fileName + ".xls"));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a table to contain the grid
                Table table = new Table();

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

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

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

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

                //Control 'Content_ctlDisplayIssues_gvIssues' of type 'GridView' must be placed inside a form tag with runat=server.
                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                parentControl.Controls.Add(form);
                form.Controls.Add(gv);
                form.RenderControl(htw);

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

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }
            else if (current is Image)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as Image).AlternateText));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}

我在这里遗漏了什么吗?

【问题讨论】:

  • 我在 Current.Response.End 之前注意到 1 change your Response.ContentType = "application/vnd.ms-excel"; 2nd 的两件事,把 HttpContext.Current.Response.Flush(); 放在看看是否有帮助
  • 在添加标题之前添加以下内容HttpContext.Current.Response.ClearContent(); and HttpContext.Current.Response.ClearHeaders(); 然后添加此行HttpContext.Current.Response.ContentType = "application/ms-excel"; 然后添加用于添加标题的行。
  • 让我试一试。
  • 更改后没有下载,而是重定向到空白页面。
  • 我使用 OpenXML 和 CloseXML 来做,如果你下载它,我实际上可以给/展示一个更简单的方法来告诉你如何在 web 表单中做到这一点,它会为你省去很多麻烦.. OpenXML Assembly 也可以免费下载。并且您可以直接使用 Datatable 以及我编写的自定义代码来做到这一点..

标签: c# asp.net excel export


【解决方案1】:

您是否曾经在记事本中打开过 Excel 文件?里面不是HTML!然而,您正在使用 HtmlTextWriter 将数据流式传输到浏览器。我认为这行不通。

如果你真的决定将数据转换成 Excel 原生格式,可以使用Excel interop,也可以寻找更精简的第三方转换工具。

如果您只需要用户在 Excel 中打开电子表格,但您不关心数据如何传输到浏览器,我建议您使用类似 @987654322 中的技术以 CSV 格式流式传输数据@。

【讨论】:

    【解决方案2】:

    大部分代码是正确的。这里的问题是 HTML Writer 没有正确呈现。 John Wu 在他的回答中提到了这一点。所以这里的走动很简单:

    代替;

    //Control 'Content_ctlDisplayIssues_gvIssues' of type 'GridView' must be placed inside a form tag with runat=server.
    System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
    parentControl.Controls.Add(form);
    form.Controls.Add(gv);
    form.RenderControl(htw);
    

    我用过;

    //here the table is rendered into the html writer
    table.RenderControl(htw);
    

    同样重要的是,在 Web.config 文件中添加这段代码

    <add key="PageInspector:ServerCodeMappingSupport" value="Disabled" />
    

    这对我来说是无缝的。

    【讨论】:

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