Html转Word文档,解决无法保存网络图片的问题

  最近项目中需要这个功能,网上有很多word转html的方法,但是html转word的方法很少,因为html中的图片转换到本地比较麻烦;

  开始的时候只能转换不带图片的html内容,但是不符合要求,将html页面中的图片改成绝对路径后在断网之后无法查看,将图片下载下来改成绝对路径后,换台机器无法观看,问题干扰了一天;

  当然有一种实现方式是将外链样式和外链图片全部一个个请求下来再放到word中排版,这个貌似非常麻烦,跟做一个浏览器一样。

  后来发现,在网站中直接复制网页然后到word文档中粘贴,可以把图片和样式全部拿过来,于是想到一种方法是否可以利用剪切板来取数据,模拟复制粘贴,最终发现可行,唯一的不足是由于宽度原因,拿来的东西在word中呈现会把格局变掉。

  代码还是非常简单,比较好理解的,下面上代码:

  

 1      public void HtmlToWordByUrl(string url)
 2         {
 3             WebBrowser WB = new WebBrowser();//新建内置浏览
 4             WB.Navigate(url);//加载页面
 5             //加载完成
 6             while (WB.ReadyState != WebBrowserReadyState.Complete)
 7             {
 8                 System.Windows.Forms.Application.DoEvents();
 9             }
10             //对加载完成的页面进行全选和复制操作
11             HtmlDocument doc = WB.Document;
12             doc.ExecCommand("SelectAll", false, "");//全选
13             doc.ExecCommand("Copy", false, "");//复制
14             //放入剪切板
15             IDataObject iData = Clipboard.GetDataObject();
16             SaveWord();//保存为word文档
17             //读取文档,下载文档
18             FileStream fs = new FileStream(Server.MapPath("~/UploadFile/test.doc"), FileMode.Open);
19             byte[] bytes = new byte[(int)fs.Length];
20             fs.Read(bytes, 0, bytes.Length);
21             fs.Close();
22             Response.ContentType = "application/octet-stream";
23             //通知浏览器下载文件而不是打开 
24             Response.AddHeader("Content-Disposition", "attachment; filename=htmlfile.doc");
25             Response.BinaryWrite(bytes);
26             WB.Dispose();
27             Response.Flush();
28             Response.End();
29 
30         }
31 
32         public void SaveWord()
33         {
34             object path;                      //声明文件路径变量
35             //string wordstr = wdstr;                   //声明word文档内容
36             MSWord.Application wordApp;       //声明word应用程序变量
37             MSWord.Document worddoc;          //声明word文档变量    
38 
39             //初始化变量
40             object Nothing = Missing.Value;                       //COM调用时用于占位
41             object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文档的保存格式
42             wordApp = new MSWord.ApplicationClass();              //声明一个wordAPP对象
43             worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing,
44                 ref Nothing, ref Nothing);
45 
46             //页面设置
47             worddoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
48             worddoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
49 
50 
51             //向文档中写入内容(直接粘贴)
52             worddoc.Paragraphs.Last.Range.Paste();
53 
54             //保存文档
55             path = Server.MapPath("~/UploadFile/test.doc");          //设置文件保存路劲
56             worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing,
57                 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
58                 ref Nothing, ref Nothing, ref Nothing, ref Nothing);
59 
60             //关闭文档
61             worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //关闭worddoc文档对象
62             wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //关闭wordApp组对象
63 
64         }

其中要注意的一点是,因为在webform页面调用webbrowser,需要引入以下引用

 1 using System.Windows.Forms; 

前端页面引用,需要加入AspCompat="true"

1 <%@ Page Language="C#" AutoEventWireup="true" AspCompat="true" CodeBehind="HtmlToWord.aspx.cs" Inherits="NurseManage.Export.HtmlToWord" %>

最后引用了微软的操作类库

 1 using MSWord = Microsoft.Office.Interop.Word; 

方法引用:

1      protected void Page_Load(object sender, EventArgs e)
2         {
3             HtmlToWordByUrl("http://www.cnblogs.com/Kuleft/p/5010636.html");
4 
5         }

效果图:

 

关于word内容排版的问题希望大家能不吝赐教,对于word的操作确实不太清楚。

还有一个就是不知道是不是百度首页(http://www.baidu.com)防盗爬,暂时转化不了。

转载于:https://www.cnblogs.com/Kuleft/p/5010636.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值