IE下用JavaScript將HTML匯出為Word、Pdf
最近升級公司內部系統發文章的功能,涉及到將文章內容匯出為html、word、pdf,系統多用於IE環境下,並且公司電腦都預裝了office,所以匯出暫時採用客戶端的方式。
頁面基本結構:
可以複製下來在瀏覽器內看下效果,我們的目標是將content內的內容分別匯出到html、word、pdf檔案中,content內的內容可能非常複雜,樣式非常多,還有可能標籤不標準,不對稱,並且有中文,如果拿到服務端去處理,比較複雜,下面分別完善三個匯出方法。<html> <head> <title>客戶端匯出測試</title> <script type="text/javascript"> function exportHtml { } function exportWord() { } function exportPdf() { } </script> </head> <body> <!-- toolbar --> <div> <button>匯出HTML</button> <button>匯出WORD</button> <button>匯出PDF</button> </div> <!-- content --> <div id="content" style="border: 1px #000 solid"> <h1>標題</h1> <font color="gray">正文內容</font> </div> </body> </html>
匯出檔案時需要選擇匯出目錄,那麼如何彈出視窗選擇目錄呢?
要使上段程式碼生效,需要對IE瀏覽器設定一下,如下圖:function getExportPath() { var shell = new ActiveXObject("Shell.Application"); var folder = shell.BrowseForFolder(0, '請選擇儲存目錄', 0x0040, 0x11); var filePath; if(folder != null) { filePath = folder.Items().Item().Path; } return filePath; }
設定完之後,直接在瀏覽器執行還可能出現沒有許可權的問題,那就需要將html部署在伺服器上,讓後將當前伺服器的訪問地址設定為可信站點。
匯出HTML:
匯出WORD:function exportHtml { var filePath = getExportPath(); if(filePath != null) { var file; try { var fso = new ActiveXObject("Scripting.FileSystemObject"); file = fso.createtextfile(filePath + "/測試匯出.html",true);// 建立檔案 file.WriteLine(content.innerHTML);// 寫入資料 alert("匯出成功"); } catch (e) { alert("匯出失敗"); } finally { if(file != null) file.close();// 關閉連線 } } }
function exportWord() {
var filePath = getExportPath();
if(filePath != null) {
try {
var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Add("", 0, 1);
var range = doc.Range(0, 1);
var sel = document.body.createTextRange();
try {
sel.moveToElementText(content);
} catch (notE) {
alert("匯出資料失敗,沒有資料可以匯出。");
window.close();
return;
}
sel.select();
sel.execCommand("Copy");
range.Paste();
//word.Application.Visible = true;// 控制word視窗是否顯示
doc.saveAs(filePath + "/匯出測試.doc");// 儲存
alert("匯出成功");
} catch (e) {
alert("匯出資料失敗,需要在客戶機器安裝Microsoft Office Word(不限版本),將當前站點加入信任站點,允許在IE中執行ActiveX控制元件。");
} finally {
try {word.quit();// 關閉word視窗} catch (ex) {}
}
}
}
匯出PDF:
var filePath = getExportPath();
if(filePath != null) {
try {
var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Add("", 0, 1);
var range = doc.Range(0, 1);
var sel = document.body.createTextRange();
try {
sel.moveToElementText(content);
} catch (notE) {
alert("匯出資料失敗,沒有資料可以匯出。");
window.close();
return;
}
sel.select();
sel.execCommand("Copy");
range.Paste();
//word.Application.Visible = true;// 控制word視窗是否顯示
doc.saveAs(filePath + "/匯出測試.pdf", 17);// 儲存為pdf格式
alert("匯出成功");
} catch (e) {
alert("匯出資料失敗,需要在客戶機器安裝Microsoft Office Word 2007以上版本,將當前站點加入信任站點,允許在IE中執行ActiveX控制元件。");
} finally {
try {word.quit();// 關閉word視窗} catch (ex) {}
}
}
匯出PDF廢了一番周折,saveAs方法有一串引數,這裡我只用到了前兩個,第一個引數是儲存檔名稱,第二個引數是儲存檔案格式,office 2007或2010支援將當前word另存為PDF格式,第二個引數是VB或C#環境下列舉類WdSaveFormat的一個值,經過多次彎曲的查詢,終於查到其各個變數對應值。
Name |
Value |
Description |
---|---|---|
wdFormatDocument |
0 |
Microsoft Office Word 97 - 2003 binary file format. |
wdFormatDOSText |
4 |
Microsoft DOS text format. |
wdFormatDOSTextLineBreaks |
5 |
Microsoft DOS text with line breaks preserved. |
wdFormatEncodedText |
7 |
Encoded text format. |
wdFormatFilteredHTML |
10 |
Filtered HTML format. |
wdFormatFlatXML |
19 |
Open XML file format saved as a single XML file. |
wdFormatFlatXML |
20 |
Open XML file format with macros enabled saved as a single XML file. |
wdFormatFlatXMLTemplate |
21 |
Open XML template format saved as a XML single file. |
wdFormatFlatXMLTemplateMacroEnabled |
22 |
Open XML template format with macros enabled saved as a single XML file. |
wdFormatOpenDocumentText |
23 |
OpenDocument Text format. |
wdFormatHTML |
8 |
Standard HTML format. |
wdFormatRTF |
6 |
Rich text format (RTF). |
wdFormatStrictOpenXMLDocument |
24 |
Strict Open XML document format. |
wdFormatTemplate |
1 |
Word template format. |
wdFormatText |
2 |
Microsoft Windows text format. |
wdFormatTextLineBreaks |
3 |
Windows text format with line breaks preserved. |
wdFormatUnicodeText |
7 |
Unicode text format. |
wdFormatWebArchive |
9 |
Web archive format. |
wdFormatXML |
11 |
Extensible Markup Language (XML) format. |
wdFormatDocument97 |
0 |
Microsoft Word 97 document format. |
wdFormatDocumentDefault |
16 |
Word default document file format. For Word 2010, this is the DOCX format. |
wdFormatPDF |
17 |
PDF format. |
wdFormatTemplate97 |
1 |
Word 97 template format. |
wdFormatXMLDocument |
12 |
XML document format. |
wdFormatXMLDocumentMacroEnabled |
13 |
XML document format with macros enabled. |
wdFormatXMLTemplate |
14 |
XML template format. |
wdFormatXMLTemplateMacroEnabled |
15 |
XML template format with macros enabled. |
wdFormatXPS |
18 |
XPS format. |
優點:原樣匯出,程式碼簡單,不用為樣式複雜的HTML匯出發愁;
缺點:依賴客戶端,只能在IE下使用,瀏覽器安全降低。
好了,先寫這麼多,大家晚安。