1. 程式人生 > >生成word的兩種方法 freemaker和POI

生成word的兩種方法 freemaker和POI

/**

*〈簡述〉 下載word文件
*〈詳細描述〉
* @author zqq
* @param yjEreport
* @throws Exception
*/
    public void exportSimpleWord(YjEreportEntity yjEreport,YjReportIssuedEntity yjReportIssued) throws Exception{  
        //Blank Document
        XWPFDocument document= new XWPFDocument();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        String path = yjEreport.getTitle()+format; 
        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File(path+".doc"));
        //新增標題
        XWPFParagraph titleParagraph = document.createParagraph();
        //設定段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);


        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("關於"+yjEreport.getTitle()+"的報告");
        titleParagraphRun.setFontSize(22);
        titleParagraphRun.setFontFamily("宋體");
        //上報單位
        String unitId = yjReportIssued.getUnitId();
//查詢上報單位
String reportUnit = yjEreportService.getUnitById(unitId);
log.info("上報單位為==="+reportUnit);
//查詢上報相關人員
        String memberId = yjReportIssued.getMemberId();
List<YjUnitMemberEntity> list = yjReportIssuedServiceI.findHql("From YjUnitMemberEntity as ch where ch.id = ?" , memberId); 
//查詢聯絡人和聯絡方式
String memberName= "";
String rePhone ="";
for (YjUnitMemberEntity yjUnitMemberEntity : list) {
    memberName = list.get(0).getMemberName();
    rePhone = list.get(0).getMemberPhone();
log.info("上報聯絡人和聯絡方式為"+memberName+"===="+rePhone);
}
//標題下的上報單位
        XWPFParagraph titleParagraphFu = document.createParagraph();
        XWPFRun run2 = titleParagraphFu.createRun();  
        run2.setText(reportUnit+":"+"\r");  
        run2.setFontFamily("仿宋");  
        run2.setFontSize(16);

//段落  正文
        XWPFParagraph firstParagraph = document.createParagraph();
        //設定文字內容
        String date =sdf.format(yjEreport.getPlaceTime());
        String year =date.substring(0,4)+"年";
        String month =date.substring(4,6)+"月";
        String day =date.substring(6,8)+"日";
        date = year + month + day;
        //正文內容
        String content =date+yjEreport.getRemark();
        
        //首行縮排
        firstParagraph.setIndentationFirstLine(800);
        XWPFRun r0=firstParagraph.createRun();//p1.createRun()將一個新執行追加到這一段
        r0.setText(content);
        r0.setFontFamily("仿宋");  
        r0.setFontSize(16);
        XWPFParagraph firstParagraph1 = document.createParagraph();
        //向右縮排
        firstParagraph1.setIndentationFirstLine(6200);
        XWPFRun r1=firstParagraph1.createRun();//p1.createRun()將一個新執行追加到這一段
        //年月日
        r1.setFontFamily("仿宋");  
        r1.setFontSize(16);
        r1.setText("北京市外辦");
        
        XWPFParagraph firstParagraph2 = document.createParagraph();
        //向右縮排
        firstParagraph2.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun r2=firstParagraph2.createRun();//p1.createRun()將一個新執行追加到這一段
        r2.setText(date);
        r2.setFontFamily("仿宋");  
        r2.setFontSize(16);
        XWPFParagraph firstParagraph3 = document.createParagraph();
        XWPFRun r3=firstParagraph3.createRun();//p1.createRun()將一個新執行追加到這一段
        //首行縮排
        firstParagraph3.setIndentationFirstLine(800);
        //聯絡方式
        String name ="(聯絡人:" +memberName+";"+"聯絡方式:"+rePhone+")";
        r3.setText(name);
        r3.setFontFamily("仿宋");  
        r3.setFontSize(16);
        document.write(out);
        out.close();
        log.info("create_table document written success.");
    }