IE上用JavaScript将HTML导出为Word、Pdf
最近升级公司内部系统发文章的功能,涉及到将文章内容导出为html、word、pdf,系统多用于IE环境下,并且公司电脑都预装了office,所以导出暂时采用客户端的方式。
页面基本结构:
<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>可以复制下来在浏览器内看下效果,我们的目标是将content内的内容分别导出到html、word、pdf文件中,content内的内容可能非常复杂,样式非常多,还有可能标签不标准,不对称,并且有中文,如果拿到服务端去处理,比较复杂,下面分别完善三个导出方法。
导出文件时需要选择导出目录,那么如何弹出窗口选择目录呢?
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; }要使上段代码生效,需要对IE浏览器设置一下,如下图:
设置完之后,直接在浏览器运行还可能出现没有权限的问题,那就需要将html部署在服务器上,让后将当前服务器的访问地址设置为可信站点。
导出HTML:
function exportHtml { var filePath = getExportPath(); if(filePath != null) { var file; try { var fso = new ActiveXObject("Scripting.FileSystemObject"); file = fso.createtextfile("测试导出.html",true);// 创建文件 file.WriteLine(content.innerHTML);// 写入数据 alert("导出成功"); } catch (e) { alert("导出失败"); } finally { if(file != null) file.close();// 关闭连接 } } }导出WORD:
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("导出测试.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("导出测试.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下使用,浏览器安全降低。
好了,先写这么多,大家晚安。