IE下用JavaScript将HTML导出为Word、Pdf
来源:互联网 发布:济南新风向网络 编辑:程序博客网 时间:2024/05/24 06:26
页面基本结构:
- <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>
导出文件时需要选择导出目录,那么如何弹出窗口选择目录呢?
- 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:
- 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) {}
- }
- }
- }
- 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) {}
- }
- }
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下使用,浏览器安全降低。
好了,先写这么多,大家晚安。
- IE下用JavaScript将HTML导出为Word、Pdf
- IE下用JavaScript将HTML导出为Word、Pdf
- IE浏览器下,将页面导出为word
- javascript 将HTML转为 word,pdf
- 将HTML页面部分内容导出为PDF
- javascript Html 导出为word excel
- Drupal7 当中如何将book导出为pdf/word
- 将html导出word
- 将PDF 转化 为 Word,Html 等文件
- 将word文档转换为html、PDF等
- html导出为word
- CSDN博客导出(三) - 将HTML转换为PDF
- CSDN博客导出(三) - 将HTML转换为PDF
- JS 如何将 HTML 页面导出为 PDF
- JS 如何将 HTML 页面导出为多页 PDF
- Windows下将Word文档转换为PDF文档
- 将pdf转换为word
- 将DataGridView导出为PDF
- iOS中视图切换的四种方式
- 第五届校内选拔javaA组-第六题
- Python获取并输出当前日期时间
- 102.hdu--4148--Length of S(n)
- poj3057(*二分图匹配建模)
- IE下用JavaScript将HTML导出为Word、Pdf
- There is no getter for property named 'account' in 'class java.lang.String'
- Swift 集合类
- 位图图像原图修改
- spring+mongodb 文件上传与下载实现
- UVA 10048 Audiophobia(Floyd)
- Mybatis简介(开源O/R映射框架)
- oracle的一些操作
- 103.NYOJ--DP--拦截导弹