【问题标题】:C# - HttpContext error when trying to save HTML to Word in C#C# - 尝试在 C# 中将 HTML 保存到 Word 时出现 HttpContext 错误
【发布时间】:2016-05-04 05:52:56
【问题描述】:

我正在用 C# 编写一个插件。我在Stringwriter 中有一个 html,我正在尝试根据我在 SO 和网络上其他地方研究过的其他示例将它输出为 Word 文档。比如这样:Exporting data from c# to word documenthttp://www.codeproject.com/Questions/196851/export-an-html-to-word-document-with-fixed-headers

对于我的一生,我无法弄清楚为什么我会在出现HttpContext 的第一行出现 NullReference 异常(实际上如何处理它是我所坚持的)。

代码如下:

using System.Web;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application//ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", @"attachment; filename=D:\WORK_ORDER.doc");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF32;
HttpContext.Current.Response.Charset = "UTF-16";
// HttpContext.Current.EnableViewState = false;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
HttpContext.Current.Response.Output.Write(swSource.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

我相信这太简单了。有人可以指出我在这里缺少什么吗?谢谢。

更新:我已经发布了堆栈跟踪,但它没有提供信息,而且很长。附加信息更能说明问题:

附加信息:对象引用未设置为对象的实例。

所有类似的例子都只显示了这段代码,没有实例化,所以我不知道怎么做,所以我很感激一些建议。

更新:

根据批准的答案,导入参考库System.Web 并不意味着您可以在非网络应用程序中使用此HttpContext 对象。

请参阅下面发布的我的解决方案作为答案。

【问题讨论】:

  • 你究竟是从哪里得到 NullReferenceException 的?
  • 就像 Manfred 所问的,能否请您显示堆栈跟踪?
  • swSource 来自哪里?
  • 旁注:HTML 不是 DOC……和 UTF32/UTF16/UTF8 不是一回事……
  • NullReference 异常在第一行:HttpContext.Current.Response.Clear();

标签: c# html ms-word


【解决方案1】:

如果我猜的话,我会说您的项目是一个类库或一些非 Web 应用程序项目。

如果是这种情况,您将无法访问 httpcontext,并且您必须将您的项目更改为 Web 项目,或者您可以查看保存文件的其他方法。例如: Writing Text File to Folder on Desktop

*注意我没有尝试过建议的解决方案。

【讨论】:

  • 非常感谢。我认为这只是解决了问题。我基本上是在写一个 UserContol,而不是 Web 应用程序。只是没有地方说我不能在非网络应用项目中使用这个对象。
  • 没问题。然而,它以一种迂回的方式说你不能通过空引用异常来使用它。如果是有效的使用场景,则 Httpcontext 永远不应为 null。
【解决方案2】:

根据 Lee 的回答,解决方案是根本不使用 HttpContext

通过将上面的 sn-p 替换为以下代码,直接将此 Word 文档写入文件(并且效果很好):

StringBuilder sb1 = new StringBuilder();
StringWriter swSource = new StringWriter(sb1);

//load swSource here with an HTML through an XSL Transformation

sb1.Replace("<html>","<html xmlns:o='urn:schemas-microsoft-com:office:office'" +
"xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><title>Time</title>"+
"<!--[if gte mso 9]><xml><w:WordDocument>"+
"<w:View>Print</w:View><w:Zoom>100</w:Zoom>"+
"<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->");

System.IO.StreamWriter writer = new System.IO.StreamWriter(@"D:\Test.doc", false, Encoding.Unicode); //open the file for writing.               
writer.Write(sb1.ToString()); //write the current date to the file. change this with your date or something.
writer.Close();

我在此处发布此信息,以便如果像我这样的其他人相信他们可以通过引用 System.Web 库来使用 HttpContext,则数据可用。

【讨论】:

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