网站建设网络推广公司,百度wordpress安装,怎么用nat做网站,模仿网站 素材哪里来最近项目中需要这个功能#xff0c;网上有很多word转html的方法#xff0c;但是html转word的方法很少#xff0c;因为html中的图片转换到本地比较麻烦#xff1b; 开始的时候只能转换不带图片的html内容#xff0c;但是不符合要求#xff0c;将html页面中的图片改成绝对路… 最近项目中需要这个功能网上有很多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; filenamehtmlfile.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; 前端页面引用需要加入AspCompattrue 1 % Page LanguageC# AutoEventWireuptrue AspCompattrue CodeBehindHtmlToWord.aspx.cs InheritsNurseManage.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