指定html内容下载为word文档
解决思路是:获取html内容并传到后台,后台把html内容转换为输入流再传给浏览器,浏览器直接下载
1.获取html内容并传到后台
$("#zxjdck .ad-xzzy-anniu").click(function(){
//文件名
var filename = $("#zxjdck .ad-jrzy-name").text();(例如:test.doc)
//文件内容
var zyHtml = '<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:docPr>
<w:view w:val="print"/><w:zoom w:percent="150"/>
</w:docPr><w:body>';
zyHtml += '<w:p><w:pPr><w:outlineLvl w:val="'+outlineLvl+'"/>
<w:spacing w:line="400" w:lineRule="auto"/></w:pPr>
<w:r><w:rPr><w:b w:val="on"/></w:rPr>
<w:t>' + 文件内容+'</w:t>/w:r></w:p>';
//模拟表单提交
var html = '';
html += '<form id="downForm" action="zxzyjd!htmlToWord.action" method="post" style="display:none">' ;
html +='<input type="hidden" name="zyHtml" value="'+zyHtml+'"></input>';
html += '<input type="hidden" name="fileName" value="'+encodeURI(filename)+'.doc"></input>';
html += '</form>';
$("#zxjdck").append(html);
$("#downForm").submit();
其中zyHtml部分使用wordXML处理,其中
encodeURI(filename) 为解决IE浏览器下载时文件名乱码问题。
<w:view w:val="print"/><w:zoom w:percent="150"/>表示默认打开为“页面视图”,缩放比例为150%。
具体请参考:
http://www.cnblogs.com/forlina/archive/2011/06/09/2076559.html
http://www.microsoft.com/china/msdn/library/office/office/XMLOfficeWord2003.mspx?mfr=true
2.后台把html内容转换为输入流再传给浏览器:
我使用的是注解方式配置的struts2。
在action中加入注解:
@Result(name = "download" , type = org.apache.struts2.dispatcher.StreamResult.class , value = "inputStream",
params = {"contentDisposition","attachment;filename=\"${downloadFileName}\"",
"inputName","inputStream",
"contentType", "application/octet-stream",
"bufferSize","4096"
})
action中方法如下:
private String fileName;
private String zyHtml;
public String getZyHtml() {
return zyHtml;
}
public void setZyHtml(String zyHtml) {
this.zyHtml = zyHtml;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String htmlToWord(){
if(fileName!=null){
setFileName(fileName);
}else{
setFileName("在线作业.doc");
}
return "download";
}
public String getDownloadFileName() {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
return fileName;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
public InputStream getInputStream(){
try {
byte b[] = zyHtml.getBytes("UTF-8");
ByteArrayInputStream bais = new ByteArrayInputStream(b);
return bais;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
通过fileName和zyHtml的get,set方法获得参数。
通过htmlToWord方法找到注解的result。
result中“inputStream”对应方法getInputStream(),"downloadFileName"对用方法getDownloadFileName()。
result返回输入流给浏览器后,浏览器自动下载文件。
指定html内容下载为word文档的更多相关文章
- 将word文档A表格中的内容拷贝到word文档B表格中
Function IsFileExists(ByVal strFileName As String) As Boolean ) <> Empty Then IsFileExists = T ...
- python+selenium爬取百度文库不能下载的word文档
有些时候我们需要用到百度文库的某些文章时,却发现需要会员才能下载,很难受,其实我们可以通过爬虫的方式来获取到我们所需要的文本. 工具:python3.7+selenium+任意一款编辑器 前期准备:可 ...
- PHP将HTML的内容保存成word文档
<?php class word { function start() { ob_start(); echo '<html xmlns:o="urn:schemas-micros ...
- 批量导出access某表内容到word文档
一.需求: 需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下 二.界面,简单设计如下: 三.添加office相关引用 添加后可在解决方案资源管理器中看到: ...
- Struts2利用iText导出word文档(包含表格)以提供下载
J2EE ExcelStrutsXML 在公司实习期间,带我的老师让我实现一功能——在显示课表的页面上上点击“导出文件“时能以word文档形式下载课表.将课表导出到excel里的功能他们已经实现了, ...
- C#读取Word文档内容代码
首先要添加引用com组件:然后引用: using Word = Microsoft.Office.Interop.Word; 获取内容: /// /// 读取 word文档 返回内容 /// //// ...
- Java 添加、读取、修改、删除Word文档属性
Word文档属性包括常规.摘要.统计.内容.自定义等,其中摘要包括标题.主题.作者.经理.单位.类别.关键词.备注等项目,通过设置这些摘要信息或自定义属性可方便对文档的管理.本文中将主要介绍对文档摘要 ...
- Java 读取txt文件生成Word文档
本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...
- C# 读取txt文件生成Word文档
本文将以C#程序代码为例介绍如何来读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: Visual Studio 2017 .Net Framework 4.6.1 ...
随机推荐
- 在ubuntu10.0.4下更新git
今天想到要在ubuntu10.0.4下下载android的源码学习一下.源码下载用到了git.以前安装过git以为应该没什么问题的,没想到报了 “fatal: git 1.7.2 or later r ...
- mvn 一些操作
拷贝依赖包 mvn dependency:copy-dependencies -DoutputDirectory=src/main/webapp/WEB-INF/lib -DincludeScope ...
- EMV标准
EMV标准是由国际三大银行卡组织--Europay(欧陆卡,已被万事达收购).MasterCard(万事达卡)和Visa(维萨)共同发起制定的银行卡从磁条卡向智能IC卡转移的技术标准,是基于IC卡的金 ...
- Annotation 与 HttpClient(5)--Annotation HttpClient
Annotation HttpClient 本内容不保证正确性,如有问题请及时提出 经过前面四篇博客的铺垫,现在给出带有标记的HttpClient的实现. 1. 带标记的HttpClient的 ...
- C++STL_类模板举例
#include<stdio.h> #include<typeinfo.h> template <class T1,class T2> class A{ T1 i; ...
- leetcode_question_111 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Memcached基本架构和思想
Memcached采用客户端-服务器的架构,客户端和服务器端的通讯使用自定义的协议标准,只要满足协议格式要求,客户端Library可以用任何语言实现. 从用户的角度来说,服务器维护了一个键-值关系的数 ...
- javascript高级知识点——内置对象原型
代码信息来自于http://ejohn.org/apps/learn/. 可以修改内置对象的方法. if (!Array.prototype.forEach) { Array.prototype.fo ...
- iOS的Ping++支付接入步骤(详细)
Ping++ SDK 代码下载地址: https://github.com/CoderLeezhen/PingppDemo 参考链接: https://www.pingxx.com/guidance/ ...
- OC中协议的概念以及用法
OC中协议的概念以及用法,协议也是OC中的一个重点,Foundation框架以及我们后面在写代码都会用到. OC中的协议就是相当于Java中的接口(抽象类),只不过OC中的名字更形象点,因为我们在学习 ...