指定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 ...
随机推荐
- vb combobox 用法问题总结
问题一 combobox 通过type类型,如下代码,通过选取name名称(改变combobox的名称)得到 其Id Type User id As Integer userName As Strin ...
- Almost Prime
Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- jdk1.6,jdk1.7共存
当然可以,安装的时候记得选择不同的安装目录,安装好以后,可以在开发工具(如eclipse)中切换不同的编译环境和运行环境.其实只要安装eclipse就自带了jdk1.3-1.6的编译环境了. Mac下 ...
- 第52周四ApplicationContext
ApplicationContext的中文意思是"应用前后关系"应用上下文即容器,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持.资 ...
- 海康威视研究院ImageNet2016竞赛经验分享
原文链接:https://zhuanlan.zhihu.com/p/23249000 目录 场景分类 数据增强 数据增强对最后的识别性能和泛化能力都有着非常重要的作用.我们使用下面这些数据增强方法. ...
- 2014ACM/ICPC亚洲区广州站 Song Jiang's rank list
欢迎参加——每周六晚的BestCoder(有米!) Song Jiang's rank list Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- [置顶] 有关ListIterator接口的add与remove方法探究
ListIterator接口继承自Iterator接口,新增了add()等方法. 关于ListIterator的add()方法的作用(接口是没有方法实现的,但其实现类对于add()方法的实现机制大致相 ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- 简单实现仿UITabBarController界面
第一步:添加两个占位View 第二步:添加子控制器 第三步:添加按钮 #import "ViewController.h" #define SCREEN_WIDTH ([UIScr ...
- HBASE学习笔记--shell
Hbase shell 启动 下载安装hbase client包,填写相关配置后进入,配置相关说明可以参见配置篇 指令:./bin/hbase shell 状态及配置信息查询 查询服务器状态 stat ...