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();
  }
 }

 }

原文地址:https://www.cnblogs.com/shijiaoyun/p/3929809.html