最近項目中需要這個功能,網上有很多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)防盜爬,暫時轉化不了。