java导出word文件
java导出word文件
test5.ftl文件生存方法,
第一步:用word新建test5.doc,填写完整模板,将需导出数据用${}代替
第二步:将test5.doc另存为test5.xml
第三部:将test5.xml重命名为test5.ftl
第四步:用记事本打开test5.ftl,在${}大括号之间填写相应的属性即可
1.jsp中的js代码
//导表
function exportExcel(){
window.location.href="<%=basePath%>student!myExportWord.action";
}
2、action中的代码(StudentAction.java)
public void myExportWord(){
Student student = new Student();
student.setName("张国荣");
student.setCode("123456789");
student.setAge("45");
student.setGrade("gradeOne");
student.setTest("国际巨星");
student.setXingbie("man");
try {
DocumentHandler dh = new DocumentHandler();
String wordName = "学生信息表";
dh.createDoc(student, "test5.ftl", getResponse(), wordName);
} catch (IOException e) {
e.printStackTrace();
}
}
3、DocumentHandler工具类代码如下:
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.huating.wgsrpt.report.model.Lawpunishment;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocumentHandler
{
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public void createDoc(Object bean,String url,HttpServletResponse response,String filepath) throws IOException {
//把对象变成map
Class<? extends Object> type = bean.getClass();
Map<String, Object> dataMap = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if(result !=null)
{
dataMap.put(propertyName,result+"");
}
else{
dataMap.put(propertyName,"");
}
}
}
} catch (IntrospectionException e) {
throw new RuntimeException("分析类属性失败", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("分析类属性失败", e);
} catch (InvocationTargetException e) {
throw new RuntimeException("分析类属性失败", e);
}
//要填入模本的数据文件
System.out.println(dataMap.toString());
configuration.setClassForTemplateLoading(this.getClass(), "/wordxml/");//表示src下的wordxml目录
Template t=null;
try {
//test.ftl为要装载的模板
t = configuration.getTemplate(url);
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
response.setCharacterEncoding("UTF-8");
filepath=java.net.URLEncoder.encode(filepath+".doc", "UTF-8");
response.setContentType("application/msword");
response.setHeader("Content-disposition", "attachment; filename="+new String(filepath.getBytes("UTF-8"),"GBK"));
Writer out = response.getWriter();
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java导出word文件的更多相关文章
- java导出word的6种方式(复制来的文章)
来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...
- [转载]java导出word的5种方式
在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...
- [原创]java导出word的5种方式
在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...
- java导出word的6种方式(转发)
来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...
- Java读写Word文件常用技术
Java操作操作Word文件,最近花了几天时间解决使用Word模板导出数据的问题,收集到一些资料分享下. 常见的技术如下: 1.POI(兼容doc.docx文件) 官方网站:http://poi. ...
- 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)
https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...
- java导出word直接下载
导出word工具类 package util; import java.io.IOException; import java.io.Writer; import java.util.Map; imp ...
- java 导出word 并下载
记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...
- WordUtil java导出word工具类
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter ...
随机推荐
- mybatis 一二事(2) - 动态代理
db.properties 单独提取出来的数据库配置,方便以后维护管理 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhos ...
- 【Android】5.7 图片库(Galery)
分类:C#.Android.VS2015: 创建日期:2016-02-07 一.简介 图库(也叫画廊)是一个布局小部件,用于在可水平滚动的列表中显示每一副图片,当前所选的图片将置于视图的中心. 注意: ...
- Linux debugger lldb
https://lldb.llvm.org/ https://lldb.llvm.org/lldb-gdb.html https://lldb.llvm.org/tutorial.html
- 黑客编程教程(六)Windows的关机和重起
第六节 Windows的关机和重起 很多木马都有远程关机功能,但这并不是一个很好的功能.不过对于入侵服务器,有时需要重起服务器.其实对于关机和重起,只需要调用几个 API函数即可实现. 对于WIN9X ...
- 二分箭术--G&K稳健过神思路
自从资料片开始,一直在思考稳定可靠的过神思路,现在有眉目了,试验了几把感觉不错,先分享如下: 1)只开2个分城,特殊情况除外.Re: 经过多次打的经验,开2个分城比开3个分城更容易选址,政策更快,快乐 ...
- 解析html文档的java库及范例
用这个工具jsoup <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <v ...
- LeetCode: Gray Code 解题报告
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...
- 【C++程序员学 python】python 之变量
既然学过C++,那么就应该知道变量是什么,常量是什么. python 相比于C++,在使用变量之前不用先声明. 而是直接使用,python 会根据你的变量自动识别其类型. 假如a = 123 那么a ...
- 一款html拼图游戏详解
本文是爱编程原创翻译,转载请看清文末的转载要求,谢谢合作! 游戏介绍 这篇文章是献给web游戏开发者用简单的开发工具开发一款游戏.此文介绍了用html.css.javascript只需简单和几个步骤开 ...
- 人工智能时代,应立即学习python
人工智能时代,应立即学习python 应用:web开发,自动化运维开发,自动化测试,数据分析,机器学习 1.python 快速易学习2.python 基于web开发(zhihu:tornad web框 ...