码迷,mamicode.com
首页 > 其他好文 > 详细

用户WORD模板写文件

时间:2017-02-27 11:51:26      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:技术分享   template   模板   ati   输出流   输出   fileinput   use   format   

技术分享

 

 

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
 
public static String createComplainWord(List<Complain> list){ 
           try {
            String templatePath = Global. getWebProjectPath() + File.separator ++ "compTemplete.doc"; 
                  InputStream is = new FileInputStream(templatePath); 
                  HWPFDocument doc = new HWPFDocument( is); 
                  Range range = doc.getRange();
                  Complain complain = list.get(0);
                  //把range范围内的${param}替换为对应参数
                  range.replaceText( "${incidentDate}", DateUtils.formatDate(complain.getIncidentDate(), "yyyy-MM-dd HH:mm:ss")); 
                  range.replaceText( "${location}", complain.getLocation()); 
                  range.replaceText( "${content}", complain.getContent()); 
                  range.replaceText( "${name}", complain.getName());
                  range.replaceText( "${phone}", complain.getPhone());
                  range.replaceText( "${isPublic}", "1".equals(complain .getIsPublic()) ? "" : "" );
                  String fileName = Global. getWebProjectPath() + File.separator + "ss ".doc" ;
                  File tempFile = new File( fileName);
                  if ( tempFile.exists()) {
                            tempFile.delete();
                  }
                  tempFile.createNewFile();
                  OutputStream os = new FileOutputStream(fileName);
                  //把doc输出到输出流中 
                  doc.write( os); 
                  closeStream(os); 
                  closeStream(is);
                  return list.get(0).getTitle() + ".doc";
           } catch (Exception e) {
                 logger.error( "exportWordError:" + e );
                 e.printStackTrace();
                 return null;
           } 
       }  
     
       /**
         * 关闭输入流
         * @param is
         */  
       private static void closeStream(InputStream is) { 
           if ( is != null) { 
              try { 
                 is.close(); 
              } catch (IOException e) { 
                 e.printStackTrace(); 
              } 
           } 
        } 
       
        /**
         * 关闭输出流
         * @param os
         */  
        private static void closeStream(OutputStream os) { 
           if ( os != null) { 
              try { 
                 os.close(); 
              } catch (IOException e) { 
                 e.printStackTrace(); 
              } 
           } 
        } 

 

用户WORD模板写文件

标签:技术分享   template   模板   ati   输出流   输出   fileinput   use   format   

原文地址:http://www.cnblogs.com/leonkobe/p/6473036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!