指定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 ...
随机推荐
- Oracle日志性能查看
http://blog.chinaunix.net/uid-20784775-id-373968.html http://www.orafaq.com/wiki/Scripts https://com ...
- Linux软件大全
https://www.linux-apps.com/browse/cat/239/ord/latest/http://www.cnblogs.com/riskyer/p/3262745.htmlht ...
- 拓扑排序(TopologicalSort)算法
拓扑排序算法应用: 有些事情做都需要按照流程的去做,比如你准备约你小女友去影院看速度与激情7大片,首先你想的是我怎么到达影院,然后达到影院,你可以先买票,或者等小女友来了一起买票,然后一起进电影大厅. ...
- poj2924---高斯求和
#include <stdio.h> #include <stdlib.h> int main() { ; long long ans,a,b; scanf("%d& ...
- Android系统移植与调试之------->如何修改Android设备的开机第一阶段Logo
1.切换到~/mx0831-0525/device/other/TBDG1073/res_pack目录下 2.更换bootup和poweron文件 找一张bmp16位的图片去除后缀名将这两张都替换,转 ...
- hdu 5586 Sum(dp+技巧)
Problem Description There )mod10007.After that,the sum of n numbers should be as much as possible.Wh ...
- hdu 4597 Play Game(区间dp,记忆化搜索)
Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...
- Spring中自己主动装配
自己主动装配 在我们了解过constructor-arg和property装配中.都须要配置对应的属性和值或者引用,假设在比較复杂的项目中.就会使得XML的配置变得复杂,自己主动装配能够使用较少的配置 ...
- poj 2686 Traveling by Stagecoach ---状态压缩DP
题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...
- JDBC操作封装
这两天学习了一下jdbc的封装,依据的是下面这篇 http://wenku.baidu.com/link?url=FaFDmQouYkKO24ApATHYmA5QzUcj-UE-7RSSZaBWPqk ...