【问题标题】:Error while converting grid data to excel将网格数据转换为 Excel 时出错
【发布时间】:2009-03-26 15:05:13
【问题描述】:

这是代码:

TextWriter writer = null;

HttpResponse response = new HttpResponse(writer);

response.ClearContent();
response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
// response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
HtmlForm htmlForm = new HtmlForm();
GridView1.Parent.Controls.Add(htmlForm);
htmlForm.Attributes["runat"] = "server";
htmlForm.Controls.Add(GridView1);
htmlForm.RenderControl(htmlTextWriter);

response.Write(stringWriter.ToString());

如果我运行此代码,它会引发 NullReferenceException。我在业务逻辑层中使用了这些代码

【问题讨论】:

  • 我理解你的问题并不难确定需要你什么

标签: c# .net asp.net excel gridview


【解决方案1】:

您在点击response.Write 行时遇到运行时错误,因为您将 null 作为响应流传入:

TextWriter writer = null;
HttpResponse response = new HttpResponse(writer);

您发布的代码非常草率。我建议您从头开始重写该方法。如果不这样做,您可能会在通过此错误后遇到其他运行时错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多