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 ...
随机推荐
- ny37 回文字符串
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...
- 基于python的接口测试框架设计(三)接口测试的框架
基于python的接口测试框架设计(三)接口测试的框架 其实我这里用到的是unittest单元测试框架,,这个框架好就好在比较清楚,,setup terdown都可以处理一些初始化及完成后的工作 主要 ...
- 解决Ubuntu16的风扇高速旋转问题(双显卡)
问题描述 自从我的双显卡的笔记本装上Ubuntu 14 后,风扇狂转.发热巨大.网上一搜索估计是显卡驱动不太行.最近英伟达的Nvidia Prime可以完美地切换双显卡,安装这个软件后,风扇就不会狂转 ...
- 一款由jQuery实现的手风琴式相册图片展开效果
之前我们有分享过很多jQuery手风琴样式的菜单,比如CSS3手风琴下拉菜单.今天要分享的jQuery手风琴效果很特别,它是手风琴样式的相册图片展开效果.我们只需点击图片缩略图即可展开当前的图片,并将 ...
- zookeeper原生API做java客户端
简介 本文是使用apache提供的原生api做zookeeper客户端 jar包 zookeeper-3.4.5.jar Demo package bjsxt.zookeeper.base; im ...
- 谈谈 老罗和"锤子"
老罗就不用多说了,搞科技公司的相声演员(笑).当时锤子公布会我看了直播.就是想看看在微博上吹翻了的手机究竟有多厉害.或者仅仅是一个笑话,事实上也希望国产手机可以有所突破,而不是一致的拼配置和简单的搞点 ...
- Spring中@Resource与@Autowired
问题 这其实就是@Autoware与@Resource没有正确的使用,这个错误是因为wmPoiOplogService这个变量装配方式是@Resource,按照@Resource的按名字查找的方式,并 ...
- java并发编程()阻塞方法与中断方法
看完这篇,我感觉我对java多线程又懵逼了. 线程可能会阻塞或暂停执行,原因有多种: 等待I/O操作结束 等待获得一个锁 等待从Thread.sleep方法中醒来 等待另一个线程计算的结果 当线程阻塞 ...
- JVM Client Server启动设置
看看你下面的这两个文件,是不是尺寸差别很大?%JAVA_HOME%/jre/bin/client/jvm.dll%JAVA_HOME%/jre/bin/server/jvm.dll Jvm动态库有 ...
- hadoop上线和下线节点
在运行中的ambari hadoop集中中动态添加或删除节点 1. 下线节点1) namenode节点上dfs.exclude文件,看配置文件怎么配置的,里每行添加一个服务器名,如我要下线server ...