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 ...
随机推荐
- 原来npm的依赖树管理不靠谱
今天删除了一个模块,结果发现项目编译无法成功,缺依赖,然后再安装也不行了.只好把node_modules清空,重新npm install,项目恢复正常. npm uninstall的时候,可能把现存某 ...
- angular学习笔记(二十七)-$http(5)-使用$http构建RESTful架构
在angular中有一个特别为RESTful架构而定制的服务,是在$http的基础上进行了封装. 但是为了学习,我们先看看用直接$http是如何构建RESTful架构的: 假设有一个银行卡的列表.需要 ...
- VS15 Visual Assist X破解
Visual Assist X是一款非常好的Microsoft Visual Studio插件,支持C/C++,C#,ASP,Visual Basic,Java和HTML等语言,Visual Assi ...
- ny36 最长公共子序列
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- Uboot的bad_save_user_regs
下面是一个宏定义,从名字“bad_save_user_regs”就可以猜测它是用来保存程序“出错”时用户态的寄存器的值. 从下面的“use bad_save_user_regs for abort/p ...
- 刷新页面要通过F5
而不是选中地址栏再按enter键,这样可能产生两种问题: 1.地址栏中的URL可能包括你上次提交的参数,你按了enter之后可能导致上次提交的参数重复提交 2.可能导致根本就没有刷新页面,刚才我修改了 ...
- RTX——第18章 内存管理
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 内存管理介绍在 ANSI C 中,可以用 malloc()和 free()2 个函数动态的分配内存和释放 ...
- 【C#】遍历List列表的同时,移除访问到的元素
需求:遍历List列表,当访问的元素符合某一条件时,将该元素移除出列表. 注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环. 例子: // 倒序遍历. for (int i ...
- Yii CDbCriteria常用用法
$criteria = new CDbCriteria;$criteria->compare('name',$this->name,true,'OR'); //like部分匹配//$cri ...
- /usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory
cc -DDEBUG -mtune=core2 -O2 \ -onvideo nvideo.c \ -I/usr/include/atk-1.0 \ -I/usr/include/cairo \ -I ...