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

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

问题描述

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



在我的生活中,我找不到为什么我会在第一行 HttpContext 出现NullReference异常(实际上如何处理它是我卡住的)。



下面是代码:

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

我相信这太简单了。有人能指出我在这里错过了什么吗?感谢。

更新:我已经发布了堆栈跟踪,但它并不冗长。附加信息更具说服力:


其他信息:未将对象引用设置为对象实例。


所有类似的例子都只显示这个代码,没有实例化,所以我不知道如何做到这一点,所以我希望得到一些建议。



更新:
$ b 根据批准的答案,发生的情况是, code> System.Web 并不意味着你可以在非web应用中使用这个 HttpContext 对象。



请参阅我在下面发表的解答作为回答。 我猜测我会说你的项目是一个类库或一些非web应用程序项目。



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



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


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

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).

Here is the code:

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.

Update:

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.

解决方案

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

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.

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

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