C# - 尝试在 C# 中将 HTML 保存到 Word 时出现 HttpContext 错误 [英] C# - HttpContext error when trying to save HTML to Word in C#

查看:23
本文介绍了C# - 尝试在 C# 中将 HTML 保存到 Word 时出现 HttpContext 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 编写一个插件.我在 Stringwriter 中有一个 html,我试图根据我在 SO 和网络上的其他地方研究的其他示例将其输出为 Word 文档.例如:将数据从c#导出到word文档http://www.codeproject.com/Questions/196851/export-an-html-to-word-document-with-fixed-headers

I am writing a plugin in c#. And I have an html in a Stringwriter and I am trying to output it as a Word document based on other samples I have researched on SO and other places on the web. Such as this: Exporting data from c# to word document and http://www.codeproject.com/Questions/196851/export-an-html-to-word-document-with-fixed-headers

在我的一生中,我无法弄清楚为什么在第一行出现 NullReference 异常 HttpContext 出现(实际上如何处理它是我遇到的问题).

For the life of me I cannot figure out why I get a NullReference exception on the first line HttpContext appears (actually how to handle it is what I am stuck on).

代码如下:

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();

我确定这太简单了.有人可以指出我在这里缺少什么吗?谢谢.

I am sure it is too simple. Can somebody point out what I am missing here? Thanks.

更新:我已经发布了堆栈跟踪,但它没有提供足够的信息.附加信息更能说明问题:

Update: I had posted the stack trace, but it is non-informative an lengthy. The additional information is more telling:

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

Additional information: Object reference not set to an instance of an object.

所有类似的例子都只展示了这段代码,没有实例化,所以我不知道如何做到这一点,所以我很感激一些建议.

All the similar examples show only this code, no instantiation, so I am at loss how to do this, so I'd appreciate some advice.

更新:

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

According to the approved answer all that happened, that importing the reference library System.Web does not mean that you can use this HttpContext object in a non-web app.

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

See my solution posted below as an answer.

推荐答案

如果我猜我会说你的项目是一个类库或一些非 Web 应用程序项目.

If I were to guess I'd say your project is a class library or some non web application project.

如果是这种情况,您将无法访问 httpcontext,您必须将项目更改为 Web 项目,或者您可以查看保存文件的其他方法.例如:将文本文件写入桌面文件夹

If this is the case you will not have access to httpcontext and you'll either have to change your project to a web project or you can look at alternative methods of saving your file. For example: Writing Text File to Folder on Desktop

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

*note I've not tried the suggested solution.

这篇关于C# - 尝试在 C# 中将 HTML 保存到 Word 时出现 HttpContext 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆