asp.net mvc-通过asp-mvc1.0创建msword文档



Hii我使用的是asp.net MVC1.0。我想通过我正在使用的功能代码创建Msword文档:

public ActionResult GetPostOffline(字符串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。。我该怎么办。。请帮我

这样尝试:

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");
}

我已经将内容类型从application/word修改为text/plain,因为您所拥有的是一个简单的sting变量(strBody(,而不是一个实际的Word文档。为了创建MSWord文档,您需要一些库。

我相信有一个更简单的解决方案。从你的问题来看,你似乎希望文件以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);
}

我添加了系统。环境标题和正文之间的换行符。不确定是否有必要。

最新更新