C# - 试图保存HTML到Word在C#中时的HttpContext不起作用 [英] C# - HttpContext does not work when trying to save HTML to Word in C#

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

问题描述

我写在C#中的插件。而且我有一个的StringWriter 的HTML,我试图把它输出的基础上我已经在网络上SO等地研究其他样本的Word文档。像这样:从C#到word文档导出数据和 HTTP://www.codeproject .COM /问题/ 196851 /出口的-HTML到字文档与固定报头



有关我的生活我不能弄清楚为什么我得到的第一行的HttpContext 出现NullReference异常(实际上是如何处理这是我坚持什么的)。



下面是代码:



<预类=郎-CS prettyprint-覆盖> 使用的System.Web;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = TRUE;
HttpContext.Current.Response.ContentType =应用// MS-词;
HttpContext.Current.Response.AddHeader(内容处置,@附件;文件名= 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 $。 b

所有类似的例子只显示该代码,没有实例化,所以我在亏损如何做到这一点,所以我会很感激一些建议。



更新:



根据核准的答案所发生的一切,使进口参考图书馆的System.Web 并不意味着你可以在非Web应用程序使用该的HttpContext 对象。



请参阅我的解决方案,下面贴作为一个答案。


解决方案

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



如果这是你将不能访问的情况下的HttpContext,你'LL要么你的项目更改为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.

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

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