通过asp mvc 1.0创建msword文档

发布于 2024-11-04 15:47:51 字数 621 浏览 2 评论 0原文

日井 我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-11-11 15:47:51

尝试这样:

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 + ".txt";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
    return File(Encoding.UTF8.GetBytes(strBody), "text/plain");
}

我已将 Content-Type 从 application/word 修改为 text/plain 因为您拥有的是一个简单的字符串变量(strBody >) 而不是实际的 Word 文档。为了创建 MSWord 文档,您将需要一些库。

Try like this:

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 + ".txt";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
    return File(Encoding.UTF8.GetBytes(strBody), "text/plain");
}

I have modified the Content-Type from application/word to text/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.

时间你老了 2024-11-11 15:47:51

我相信有一个更简单的解决方案。从您的问题来看,您似乎希望文件以 word 打开,即使它只是纯文本。这是我的解决方案:

{
    Post post = new Post();
    post = PostBLL.PostDetails(new Guid(PostId.Replace("'","")));
    string strBody =  "<body>" + post.Title + System.Environment.NewLine + post.Body + "</body>";
    string filename = post.Title + ".doc";
    return File(Encoding.UTF8.GetBytes(strBody), "application/word", filename);
}

我在标题和正文之间添加了 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:

{
    Post post = new Post();
    post = PostBLL.PostDetails(new Guid(PostId.Replace("'","")));
    string strBody =  "<body>" + post.Title + System.Environment.NewLine + post.Body + "</body>";
    string filename = post.Title + ".doc";
    return File(Encoding.UTF8.GetBytes(strBody), "application/word", filename);
}

I added the System.Environment.NewLine between the title and body. Not sure if it is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文