c# html 转word

C# html保存为word文件两种方式:

var content = "";

//如果图片或者链接不是绝对路径,需要把相对路径转化为绝对路径,否则word里面图片显出不出来
string hostpath = Request.Url.AbsoluteUri.ToString().Substring(0, Request.Url.AbsoluteUri.ToString().Length - Request.Url.LocalPath.ToString().Length + 1);
Regex rx = new Regex(@"(?<=src="")(.*?)(?="")", RegexOptions.IgnoreCase | RegexOptions.Singleline);
content = rx.Replace(content, hostpath + "$1");
rx = new Regex(@"(?<=href="")(.*?)(?="")", RegexOptions.IgnoreCase | RegexOptions.Singleline);
content = rx.Replace(content, hostpath + "$1");


//1.通过浏览器转word,直接下载
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
System.Web.HttpContext.Current.Response.ContentType = "application/msword";
System.Web.HttpContext.Current.Response.Charset = "utf-8";
System.Web.HttpContext.Current.Response.Write(content);
System.Web.HttpContext.Current.Response.End();

//2.直接选择目录地址保存为word
//var Path = "D:\\新建文件夹\\result2.doc";//word文件保存路径
//sw = new StreamWriter(Path, false, Encoding.GetEncoding("utf-8"));
//sw.WriteLine(content);
//sw.Flush();
//sw.Close();

转载于:https://www.cnblogs.com/TREN/p/9989459.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值