WORD导出

  1. public class HwpfTest {    
  2.       
  3.    @Test    
  4.    public void testWrite() throws Exception {    
  5.       String templatePath = "D:\\word\\template.doc";    
  6.       InputStream is = new FileInputStream(templatePath);    
  7.       HWPFDocument doc = new HWPFDocument(is);    
  8.       Range range = doc.getRange();    
  9.       //把range范围内的${reportDate}替换为当前的日期    
  10.       range.replaceText("${reportDate}"new SimpleDateFormat("yyyy-MM-dd").format(new Date()));    
  11.       range.replaceText("${appleAmt}""100.00");    
  12.       range.replaceText("${bananaAmt}""200.00");    
  13.       range.replaceText("${totalAmt}""300.00");    
  14.       OutputStream os = new FileOutputStream("D:\\word\\write.doc");    
  15.       //把doc输出到输出流中    
  16.       doc.write(os);    
  17.       this.closeStream(os);    
  18.       this.closeStream(is);    
  19. //浏览器导出
  20.   response.reset();
                response.setContentType("application/msword; charset=UTF-8");
    //            response.setCharacterEncoding ("utf-8");//设置编码集,文件名不会发生中文乱码
                response.setHeader("Content-Disposition", "Attachment;filename= "
                                + new String((adYear+"年"+adMonth+"月计划.doc").toString().getBytes(
                                //"UTF-8"),"UTF-8"));
                                "gb2312"), "ISO8859_1"));
                OutputStream  outstream= null;
                try {
                    outstream = response.getOutputStream();
                    document.write(outstream);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        if(tempFileInputStream!=null){
                            outstream.close();
                        }
                        if(outstream!=null){
                            outstream.close();
                        }
                    }catch (Exception io){
                        io.printStackTrace();
                    }
                }

  21.    }    
  22.       
  23.    /**  
  24.     * 关闭输入流  
  25.     * @param is  
  26.     */    
  27.    private void closeStream(InputStream is) {    
  28.       if (is != null) {    
  29.          try {    
  30.             is.close();    
  31.          } catch (IOException e) {    
  32.             e.printStackTrace();    
  33.          }    
  34.       }    
  35.    }    
  36.      
  37.    /**  
  38.     * 关闭输出流  
  39.     * @param os  
  40.     */    
  41.    private void closeStream(OutputStream os) {    
  42.       if (os != null) {    
  43.          try {    
  44.             os.close();    
  45.          } catch (IOException e) {    
  46.             e.printStackTrace();    
  47.          }    
  48.       }    
  49.    }    
  50.       
  51.      
  52. }   


1.2     实例2

需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar;poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar
* POI方式导出word文档,需要提前创建word导出模板
* POI方式导出word时,本机安装的office版本是2007以前的版本,使用到的是HWPFDocument
* POI方式导出word时,本机安装的office版本是2007+的版本,使用到的是XWPFDocument

[java]  view plain  copy
  1. package officeWordDoc;  
  2. import java.io.FileInputStream;  
  3. import java.net.URLEncoder;  
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7. import javax.faces.context.FacesContext;  
  8. import javax.servlet.ServletOutputStream;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.apache.poi.hwpf.HWPFDocument;  
  13. import org.apache.poi.hwpf.usermodel.Range;  
  14.   
  15.   
  16. /** 
  17. * @ClassName:WordDocDemo_POI 
  18. * @Description:使用POI方式实现word文档导出功能 
  19. *               需要的jar包:poi-3.10.1.jar;poi-ooxml-3.10.1.jar; 
  20. *               poi-ooxml-schemas-3.10.1.jar;poi-scratchpad-3.10.1.jar 
  21. *               POI方式导出word文档,需要提前创建word导出模板 
  22. *               POI方式导出word时,本机安装的office版本是2007以前的版本,使用到的是HWPFDocument 
  23. *               POI方式导出word时,本机安装的office版本是2007+的版本,使用到的是XWPFDocument 
  24. * @date:2017年5月12日 
  25. * 修改备注: 
  26. */  
  27. public class WordDocDemo_POI{  
  28.       
  29.     public static void main(String[] args){  
  30.         WordDocDemo_POI poi = new WordDocDemo_POI();  
  31.         poi.exportWord();  
  32.     }  
  33.   
  34.     /**  
  35.     * @Description:使用POI实现word导出功能,JSF页面请求,此处涉及到JSF框架中FacesContext对象,可摘除, 
  36.     * @date: 2017年5月12日 下午6:50:51 
  37.     * @修改备注:  
  38.     */  
  39.     public void exportWord() {  
  40.         FacesContext context = FacesContext.getCurrentInstance();  
  41.         context.responseComplete();  
  42.         HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();  
  43.         HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();  
  44.         response.reset();  
  45.         try {  
  46.             String fileName = "关于"+"eventInfo.getEventName()"+"的情况报告.doc";  
  47.             fileName = URLEncoder.encode(fileName, "UTF-8");  
  48.             response.setHeader("Content-disposition""attachment;filename="+fileName);  
  49.             response.setContentType("application/msword");  
  50.             ServletOutputStream outputStream = response.getOutputStream();  
  51.             //引入word模板,转化成流  
  52.             //String wordTemp = "E:\\项目\\导出报告模板\\reportTemplate.doc";  
  53.             String wordTemp = request.getSession().getServletContext().getRealPath("eventInfo.export.reportTemplate");  
  54.             FileInputStream inputStream = new FileInputStream(wordTemp);  
  55.             HWPFDocument document = new HWPFDocument(inputStream);  
  56.             //替换内容  
  57.             Range range = document.getRange();  
  58.             Map<String, String> content = this.getReplaceContent();  
  59.             for(java.util.Map.Entry<String, String> entry : content.entrySet()){  
  60.                 range.replaceText(entry.getKey(), entry.getValue());  
  61.             }  
  62.             document.write(outputStream);  
  63.             inputStream.close();  
  64.             outputStream.flush();  
  65.             outputStream.close();  
  66.         } catch (Exception e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.         FacesContext.getCurrentInstance().responseComplete();  
  70.     }  
  71.       
  72.     /** 
  73.      * 获取模板中需要替换的文字 
  74.      * @param report 
  75.      * @return 
  76.      */  
  77.     private Map<String, String> getReplaceContent(){  
  78.         Map<String, String> content = new HashMap<String, String>();  
  79.         content.put("${eventType}""typeName类型名称");  
  80.         content.put("${reportName}""report.getReportName报告名称");  
  81.         content.put("${reporterUnit}""report.getReporterUnit报送单位");  
  82.         content.put("${loginUser}""loginUser登录人员信息");  
  83.         content.put("${nowTime}""yyyy年MM月dd日时间消息");  
  84.         content.put("${occurTime}""yyyy年MM月dd日事发时间");  
  85.         content.put("${disaster}""非必填写1");  
  86.         content.put("${eventDesc}""非必填写2");  
  87.         content.put("${eventDesc}""非必填写3");  
  88.         return content;  
  89.     }  
  90.   

  1. }  

https://blog.csdn.net/QQ578473688/article/details/72082085?locationNum=15&fps=1

猜你喜欢

转载自blog.csdn.net/qq_41665476/article/details/80647107