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 ...
随机推荐
- WPF多线程访问控件
大家知道WPF中多线程访问UI控件时会提示UI线程的数据不能直接被其他线程访问或者修改,该怎样来做呢? 分下面两种情况 1.WinForm程序 1)第一种方法,使用委托: private delega ...
- 让一个div始终固定在页面的某一固定位置的方法
方法一:直接用position:fixed 方法二:写一个滚动条滚动事件,让这个div设置 position:absolute 该top的距离等于滚动的距离scrollTop() 写法如下:$(win ...
- iOS 键盘处理(改变键盘为完成键),UITextField键盘显示隐藏,弹出,回弹
很多时候用到UITextField时,处理键盘是一个很棘手的问题. 问题一:如何隐藏键盘? 方案1.改变键盘右下角的换行(enter)键为完成键,后实现代理方法键盘自动回弹 keyBoardContr ...
- 【iOS】TableView的footerView不随cell滚动而停留在tableView底部的问题
苹果官方给我提供TableView的FooterView和HeaderView停留在顶部的非常不错效果,有时候我们不须要这些FooterView和HeaderView停留在底部或者上部,如今就以Foo ...
- linux命令--vi,vim
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件.并将光标置于第n行首 vi + filename :打开文件.并将光标置 ...
- silverlight调用WebService传递json接收绑定数据
1.接收数据: WebService通过接口接收数据.类型为object 2.类型转换: 通过json转换方法将object转换为json格式数据 3.调用WebService方法: silverli ...
- [mmc]Linux下MMC/SD/SDIO的识别与操作
转自:http://blog.csdn.net/skdkjzz/article/details/38927943 我们知道host在扫描卡的过程中,其识别的顺序为SDIO SD MMC,并且从它的注 ...
- u-boot可ping通PC,PC不可ping通u-boot
http://blog.csdn.net/ce123_zhouwei/article/details/7339134 开发板运行U-Boot,在终端下使用Ping命令是能Ping通PC机,但PC机Pi ...
- Oracle数据误删除的恢复操作
flashbackup 闪回操作: 1. 打开表的闪回功能: alter table dw_stg.fm_user_play_d enable row movement; 2. 查询要闪回的表的记录信 ...
- Hbase 学习(一) hbase配置文件同步
最近在狂啃hadoop的书籍,这部<hbase:权威指南>就进入我的视野里面了,啃吧,因为是英文的书籍,有些个人理解不对的地方,欢迎各位拍砖. HDFS和Hbase配置同步 hbase的配 ...