• 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>

    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext-asian</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 font7 = new Font(BaseFont.createFont(), 12, Font.NORMAL);
            Font font6 = new Font(BaseFont.createFont(), 10, Font.NORMAL);

      // HeaderFooter的第2个参数为false时代表不打印页码

      //页眉如果设置两次 第一次内容将被替换,所以只能设置一次通过“ ”换行,这样做的缺点就是不能设置两种字体
            HeaderFooter header=new HeaderFooter(new Phrase("上海****科技有限公司 SHANGHAI **** TECHNOLOGY CO.,LTD.",font7),false);  
            //设置是否有边框等  
    //      header.setBorder(Rectangle.NO_BORDER);  
            document.setHeader(header);  
            HeaderFooter footer=new HeaderFooter(new Phrase("-",font6),new Phrase("-",font6));  //页脚设置页码
            // 0是靠左  1是居中 2是居右
            footer.setAlignment(1);  
            document.setFooter(footer); 
            // 页眉页脚的设置一定要在open前设置好   
            document.open();

           //itext-asian 不和itext版本匹配将报不识别字体的错
           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模式查询数据库时可见换行符为“ ”,

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

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

      此次使用版本为2.1.7

  • 相关阅读:
    (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row
    (二叉树 BFS) leetcode513. Find Bottom Left Tree Value
    (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
    (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
    (BFS) leetcode 690. Employee Importance
    (BFS/DFS) leetcode 200. Number of Islands
    (最长回文子串 线性DP) 51nod 1088 最长回文子串
    (链表 importance) leetcode 2. Add Two Numbers
    (链表 set) leetcode 817. Linked List Components
    (链表 双指针) leetcode 142. Linked List Cycle II
  • 原文地址:https://www.cnblogs.com/god-monk/p/7080948.html
Copyright © 2020-2023  润新知