• java使用IText将数据导出为pdf文件(数据为excel表格样式)


    1.pom.xml导入使用的jar包

    复制代码
           <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-pdfa</artifactId>
                <version>5.5.0</version>
            </dependency>
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>5.2.0</version>
            </dependency>
    复制代码

    2.直接上代码(数据导出为pdf文件,数据呈现样式为自定义excel表格样式,支持多页展示)

    复制代码
    public class DataToPdf {
        public static final String DEST = "pdf/tables.pdf";
    
        public static void main(String[] args) throws IOException, DocumentException {
            File file = new File(DEST);
            file.getParentFile().mkdirs();
            new DataToPdf().dataToPdf(DEST);
        }
    
        /**
         * 数据转pdf
         * @param dest
         * @throws IOException
         * @throws DocumentException
         */
        public void dataToPdf(String dest) throws IOException, DocumentException {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(dest));
            document.open();
    
            // 使用语言包字体
            BaseFont abf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            //字体
            Font font = new Font(abf, 8);
    
            //段落
            Paragraph p = new Paragraph("测试结算单", new Font(abf, 12, Font.BOLD));
            p.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(p);
    
            //表格
            PdfPTable table = new PdfPTable(8);//numcolumns:列数
            table.setSpacingBefore(16f);//表格与上面段落的空隙
    
            //表格列创建并赋值
            PdfPCell cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
            cell.disableBorderSide(13);//去除左右上边框,保留下边框
            cell.setColspan(4);//合并列数
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.disableBorderSide(13);
            cell.setColspan(3);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("单位(元)", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(13);
            cell.setColspan(1);
            table.addCell(cell);
            //首行
            cell = new PdfPCell(new Phrase("期间", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("月份", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("分类", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("年利率", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("日利率", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("基数", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("利息", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("起始日:2020-03-26\n" +
                    "结束日:2020-04-25", font));
            cell.setPadding(16f);
            cell.setVerticalAlignment(Element.ALIGN_CENTER);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setRowspan(3);
            cell.setColspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("4", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("资金", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.10%", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("0.000031", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("10598164.91", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("325.01", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("4", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("资金", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.10%", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("0.000031", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("-", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("-", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("4", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("资金", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.10%", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("0.000031", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("-", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("-", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("合计", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(7);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("325.01", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("会计制单:", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(14);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("复核:", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(14);
            cell.setColspan(4);
            table.addCell(cell);
            table.setSpacingBefore(16f);
            document.add(table);
    
            //下一页
            document.newPage();
            //段落
            Paragraph p1 = new Paragraph("下一页测试结算单", new Font(abf, 12, Font.BOLD));
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            document.add(p1);
    
            //表格
            table = new PdfPTable(8);//numcolumns:列数
            table.setSpacingBefore(16f);//表格与上面段落的空隙
    
            //表格列创建并赋值
            cell = new PdfPCell(new Phrase("单位名称:测试有限公司", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);//居中
            cell.disableBorderSide(13);//去除左右上边框,保留下边框
            cell.setColspan(4);//合并列数
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("日期:2020-06-05", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.disableBorderSide(13);
            cell.setColspan(3);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("单位(元)", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(13);
            cell.setColspan(1);
            table.addCell(cell);
            //首行
            cell = new PdfPCell(new Phrase("期间", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("月份", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("分类", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("年利率", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("日利率", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("基数", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("利息", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("起始日:2020-04-26\n" +
                    "结束日:2020-05-25", font));
            cell.setPadding(16f);
            cell.setVerticalAlignment(Element.ALIGN_CENTER);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("4", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("资金", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("1.10%", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("0.000031", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("10598164.91", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("325.01", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("合计", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setColspan(7);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("325.01", font));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
    
            cell = new PdfPCell(new Phrase("会计制单:", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(14);
            cell.setColspan(4);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("复核:", font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.disableBorderSide(14);
            cell.setColspan(4);
            table.addCell(cell);
            table.setSpacingBefore(16f);
            document.add(table);
    
            document.close();
        }
    }
    复制代码

    3.上述代码中对于IText字体设置可改:

    复制代码
    使用iText中的字体 
    
       例:BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
    
    使用Windows系统自带的字体
    
       例:BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
    
    使用外部引入的资源字体(
    
       例:BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
    复制代码

    4.上述代码生成的表格边框隐藏方式:

    复制代码
    1:代表上边框
    
    2:代表下边框
    
    4:代表左边框
    
    8:代表右边框
    //需要隐藏那些边框就把对应的值加起来,得到的和就是要设置的值
    //比如要隐藏左右边框 就是 4+8=12
    cell.disableBorderSide(12);//左右没了
    复制代码

     具体例子:

    复制代码
    //隐藏上边框
    cell.disableBorderSide(1);
    //隐藏下边框 cell.disableBorderSide(2); //隐藏上、下边框 cell.disableBorderSide(3); //隐藏左边框 cell.disableBorderSide(4); //隐藏左、上边框 cell.disableBorderSide(5); //隐藏左、下边框 cell.disableBorderSide(6); //隐藏左、上、下边框 cell.disableBorderSide(7); //隐藏右边框 cell.disableBorderSide(8); //隐藏右、上边框 cell.disableBorderSide(9); //隐藏右、下边框 cell.disableBorderSide(10); //隐藏右、上、下边框 cell.disableBorderSide(11); //隐藏左、右边框 cell.disableBorderSide(12);//左右没了 //隐藏上、左、右边框 cell.disableBorderSide(13);//只剩下 //隐藏下、左、右边框 cell.disableBorderSide(14);//只剩上 //隐藏全部 cell.disableBorderSide(15);//全没了
    复制代码

    5.运行展示效果

     例:
     转自:https://www.cnblogs.com/xianshen/p/13139435.html
     
  • 相关阅读:
    rsync
    2个网卡流量的变化
    服务器端FIN的条件
    The third column indicates whether subclasses of the class declared outside this package have access to the member.;
    Java反序列化与远程代码执行

    address sizes : 46 bits physical, 48 bits virtual
    intptr_t、uintptr_t数据类型的解析
    Why should we typedef a struct so often in C?
    源码 502 回溯
  • 原文地址:https://www.cnblogs.com/javalinux/p/15670203.html
Copyright © 2020-2023  润新知