html代码word,Html To Word(一)(示例代码)

Html to Word 博客中有很多文章, 我就把自己在项目中用到的,比较简单的写一下, 方便以后用到

本人采用C# mvc

1、项目中创建view 页面,

页面内容

TestHtmlToWord

Html 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

037538f5f376dfa200d25513f177705b.png

但是实际上我们要的是

d6d33a26f6460b66407ba843e905cbef.png

所以还需要在页面中添加

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">

最终页面

TestHtmlToWorld

Html To Word Test

后期可能还会有其他的方法, 待完善...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值