首页 > 代码库 > itext生成Word

itext生成Word

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext-rtf</artifactId>
    <version>2.1.7</version>
</dependency>

List<HashMap<String, int[]>> list = troubleLocationInt(year, month);
        String path = request.getSession().getServletContext().getRealPath("/fileDisk/report/word");
        String fileName="/月度工作总结.doc";
        File filedir = new File(path);  
        if (!filedir.exists()) {  
            filedir.mkdirs();  
        }
        File file = new File(path+fileName);
        if(file.exists()&& file.isFile()){
            file.delete();
        }
       
        Document document = new Document(PageSize.A4,75,75,70,70);
        RtfWriter2 writer = RtfWriter2.getInstance(document,new FileOutputStream(path +fileName));
       //页眉
        /*Font font5 = new Font(BaseFont.createFont(), 17, Font.BOLD);
        Font font7 = new Font(BaseFont.createFont(), 12, Font.NORMAL);
         Paragraph footerPara1 = new Paragraph("上海****科技有限公司", font5);     
         Paragraph footerPara2 = new Paragraph("SHANGHAI **** TECHNOLOGY CO.,LTD.", font7);     
         Paragraph footerPara = new Paragraph();     
       footerPara.add(footerPara1);     
       footerPara.add(footerPara2);     
       HeaderFooter header2 = new HeaderFooter(footerPara,false);   
       header2.setAlignment(HeaderFooter.LEFT);   
       document.setHeader(header2);
        //页脚
       Font font6 = new Font(BaseFont.createFont(), 10, Font.NORMAL);
        Paragraph p17 = new Paragraph();
        p17.add(new Phrase(" - "));
        p17.add(new RtfTotalPageNumber());//第几页  
        p17.add(new Phrase(" - "));
        p17.setFont(font6);
        p17.setAlignment(1);
        HeaderFooter footer = new HeaderFooter(p17,false);   
        footer.setAlignment(HeaderFooter.ALIGN_CENTER);   
        document.setFooter(footer);*/
        
        document.open();
       /* BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);*/
        BaseFont bfChinese=BaseFont.createFont();
        Font font = new Font(bfChinese, 12, Font.NORMAL); //1
        Font font3 = new Font(bfChinese, 14, Font.BOLD);//一
        Font font2 = new Font(bfChinese, 21, Font.BOLD);//大标题
        Font font4 = new Font(bfChinese, 12, Font.BOLD);//1.1 正文
        
        Paragraph p1 = new Paragraph("技术市场部"+month+"月份月度工作总结", font2);
        p1.setSpacingAfter(15);  
        p1.setSpacingBefore(15);
        p1.setAlignment(1);
        document.add(p1);
        
        Paragraph p2 = new Paragraph("一、  本月工作进展", font3);
        p1.setSpacingAfter(10);  
        p1.setSpacingBefore(10);
        p1.setAlignment(1);
        document.add(p2);
        
        Paragraph p3 = new Paragraph("1.  售后服务保障", font4);
        p1.setSpacingAfter(10);  
        p1.setSpacingBefore(10);
        p1.setAlignment(1);
        document.add(p3);


        Paragraph p4 = new Paragraph(“想要展示的段落。。。”, font);
        p1.setSpacingAfter(10);  
        p1.setSpacingBefore(10);
        p1.setAlignment(1);
        document.add(p4);
        
        //图
        String pic1Path = request.getSession().getServletContext().getRealPath("/fileDisk/report/img/pic1.png");
        Image image = Image.getInstance(pic1Path);
        image.scaleAbsolute(400,270);//控制图片大小
        document.add(image);
        
        //机车故障地理位置表
        Font font8 = new Font(bfChinese, 10, Font.NORMAL); //1
        Table table1 = new Table(13);
        table1.setWidths(new float[] { 0.16f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f,0.07f });
        table1.addCell(new Paragraph("",font8));
        table1.addCell(new Paragraph("1月",font8));
        table1.addCell(new Paragraph("2月",font8));
        table1.addCell(new Paragraph("3月",font8));
        table1.addCell(new Paragraph("4月",font8));
        table1.addCell(new Paragraph("5月",font8));
        table1.addCell(new Paragraph("6月",font8));
        table1.addCell(new Paragraph("7月",font8));
        table1.addCell(new Paragraph("8月",font8));
        table1.addCell(new Paragraph("9月",font8));
        table1.addCell(new Paragraph("10月",font8));
        table1.addCell(new Paragraph("11月",font8));
        table1.addCell(new Paragraph("12月",font8));
        document.add(table1);
        
        String text2="";

//若text2内容为页面存储到数据库中的段落文字,当debug模式查询数据库时可见换行符为“\r\n”,

  因此换行后还会有一行空行,将\r替换为“空字符串”,只留\n即可去除空行。eg:text2.replaceAll("\r", "")
        Paragraph p5 = new Paragraph(text2, font);
        p1.setSpacingAfter(10);  
        p1.setSpacingBefore(10);
        p1.setAlignment(1);
        document.add(p5);
           
        document.close();
    }
}

注意:BaseFont此次设置编码格式报错,因此未设置,“\r”在生成Word时可能不识别,会变成“?”,需要特殊处理。

  此次使用版本为2.1.7

itext生成Word