通过asp mvc 1.0创建msword文档
日井 我使用的是asp.net MVC1.0。我想通过我正在使用的函数的代码创建 Ms word 文档:
public ActionResult GetPostOffline(string PostId)
{
Post post = new Post();
post = PostBLL.PostDetails(new Guid(PostId.Replace("'","")));
string strBody = post.Title +post.Body;
string filename = post.Title + ".doc";
Response.ContentType = "application/word";
Response.AppendHeader("Content-disposition", "attachment; filename=" + filename);
Response.Write(strBody);
return View("~/Views/Posts/AllPosts.aspx");
}
它可以正确打开 Word 文档,但在该文档中它没有显示正确的内容。它不是显示内容,而是显示我网站的 HTML..我应该做什么..请帮助我
Hii
I am using asp.net MVC1.0. I want to create Ms word document through code I am using function :
public ActionResult GetPostOffline(string PostId)
{
Post post = new Post();
post = PostBLL.PostDetails(new Guid(PostId.Replace("'","")));
string strBody = post.Title +post.Body;
string filename = post.Title + ".doc";
Response.ContentType = "application/word";
Response.AppendHeader("Content-disposition", "attachment; filename=" + filename);
Response.Write(strBody);
return View("~/Views/Posts/AllPosts.aspx");
}
It is correctly opening a word document but in that document it is not showing proper content . Instead of showing content it's is displaying HTML of my website.. What should i do .. please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样:
我已将 Content-Type 从
application/word
修改为text/plain
因为您拥有的是一个简单的字符串变量(strBody
>) 而不是实际的 Word 文档。为了创建 MSWord 文档,您将需要一些库。Try like this:
I have modified the Content-Type from
application/word
totext/plain
as what you have is a simple sting variable (strBody
) and not an actual Word document. In order to create an MSWord document you will need some library.我相信有一个更简单的解决方案。从您的问题来看,您似乎希望文件以 word 打开,即使它只是纯文本。这是我的解决方案:
我在标题和正文之间添加了 System.Environment.NewLine 。不确定是否有必要。
I believe there is a simpler solution. From your question, it looks like you want the file to open up in word, even if it is just plain text. Here is my solution:
I added the System.Environment.NewLine between the title and body. Not sure if it is necessary.