Html to Word 博客中有很多文章, 我就把自己在项目中用到的,比较简单的写一下, 方便以后用到
本人采用C# mvc
1、项目中创建view 页面,
页面内容
TestHtmlToWordHtml To Word Test
控制器创建
public voidHtmlToWordTest()
{string wordContent = string.Empty;
IView view= ViewEngines.Engines.FindPartialView(ControllerContext, "TestHtmlToWorld").View;using (var writer = newStringWriter())
{var viewContext = newViewContext(ControllerContext, view, ViewData, TempData, writer);
viewContext.View.Render(viewContext, writer);
wordContent=writer.ToString();
writer.Close();
writer.Dispose();
}string name = string.Empty;string userAgent = Request.ServerVariables["http_user_agent"].ToLower();
Response.AppendHeader("Content-Disposition", "attachment;filename=TestHtmlToWorld.doc");
Response.ContentType= "application/ms-word";
Response.Charset= "utf-8";
Response.ContentEncoding= System.Text.Encoding.GetEncoding("utf-8");
Response.Write(wordContent);
Response.End();
}
下载下来后,默认打开是网页word
但是实际上我们要的是
所以还需要在页面中添加
xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">
最终页面
TestHtmlToWorldHtml To Word Test
后期可能还会有其他的方法, 待完善...