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文件的更多相关文章

  1. java导出word的6种方式(复制来的文章)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  2. [转载]java导出word的5种方式

    在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...

  3. [原创]java导出word的5种方式

    在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Ja ...

  4. java导出word的6种方式(转发)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  5. Java读写Word文件常用技术

      Java操作操作Word文件,最近花了几天时间解决使用Word模板导出数据的问题,收集到一些资料分享下. 常见的技术如下: 1.POI(兼容doc.docx文件) 官方网站:http://poi. ...

  6. 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)

    https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...

  7. java导出word直接下载

    导出word工具类 package util; import java.io.IOException; import java.io.Writer; import java.util.Map; imp ...

  8. java 导出word 并下载

    记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...

  9. WordUtil java导出word工具类

    import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter ...

随机推荐

  1. wpf程序线程与UI内容交互

    在UI线程里执行复杂的操作,会造成UI假死.常用的解决方法是开2个线程. 而新线程要调用UI里的东西,必须这么用: this.Dispatcher.Invoke(content);

  2. 【Android】自己定义ListView的Adapter报空指针异常解决方法

    刚刚使用ViewHolder的方法拉取ListView的数据,可是总会报异常. 细致查看代码.都正确. 后来打开adapter类,发现getView的返回值为null. 即return null. 将 ...

  3. nginx location URI匹配规则

    当nginx收到一个请求后,会截取请求的url部份,去搜索所有location指令中定义的URI匹配模式.在server模块中可以定义多个location指令来匹配不同的url请求,多个不同的loca ...

  4. Azure Cloud Application Design and Implementation Guidance performance-optimization

    https://github.com/mspnp/azure-guidance https://github.com/mspnp/performance-optimization https://gi ...

  5. C++ 11 std::function std::bind使用

    cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNorm ...

  6. flask celery 使用方法

    一.安装 由于celery4.0不支持window,如果在window上安装celery4.0将会出现下面的错误flask_clery 你现在只能安装pip install celery==3.1 二 ...

  7. vue2.0使用动态组件实现tab切换效果(vue-cli)

    <template> <div> <div>#动态组件实现tab切换效果#</div><br><br><br> &l ...

  8. 利用eclipse中的各种功能帮助你理解代码

    @菜单栏下面,工具栏,有一对黄色的箭头按钮,一个指向左边,一个指向右边,快捷键是Alt+Left/Alt+Right 功能是跳转到你刚刚编辑过的地方 这里的Left/Right指的是左右方向键,可以方 ...

  9. .Net应该学什么怎么学(二)

    更新时间:2012年06月05日18时23分 来源:传智播客.Net 接上篇<[我来解惑].Net应该学什么怎么学(一)>. 二.C#面向对象基础 初学者学面向对象的时候没必要(也做不到) ...

  10. iOS网络NSURLConnection使用详解

    一.整体介绍 NSURLConnection是苹果提供的原生网络访问类,但是苹果很快会将其废弃,且由NSURLSession(iOS7以后)来替代.目前使用最广泛的第三方网络框架AFNetworkin ...