生成表格pdf格式

1. 添加依赖

java 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.2.5</version>
        </dependency>

2.demo代码

java 复制代码
    @SneakyThrows
    public void export(List list, OutputStream outputStream) {
        List<JbMaintainPlanEntity> list = planEntityService.list(dto);
        // 创建 PDF 写入器和文档对象
        PdfWriter writer = new PdfWriter(outputStream);
        PdfDocument pdfDocument = new PdfDocument(writer);
        Document document = new Document(pdfDocument);
        //支持中文,指定字符集
        InputStream resourceAsStream = resourceLoader.getClassLoader().getResourceAsStream("front/simsun.ttf");
        byte[] bytes = IoUtil.readBytes(resourceAsStream);
        FontProgram fontProgram = FontProgramFactory.createFont(bytes);
        PdfFont font = PdfFontFactory.createFont(fontProgram);
        document.setFont(font);
        // 创建表格,并设置列数为data中的键的数量
        float[] columnWidths = {2f, 2f, 2f, 3f, 3f, 2f};
        Table table = new Table(columnWidths);
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 添加表头
        String[] headers = {"序号", "设备分类", "设备机型", "设备", "轮保日期", "备注"};
        for (String header : headers) {
            Cell cell = new Cell().add(new Paragraph(header));
            cell.setTextAlignment(TextAlignment.CENTER);
            table.addHeaderCell(cell);
        }
        Integer count = 1;
        for (Objetc data: list) {
            Map<String, Object> map = new HashMap<>();
            BeanUtil.beanToMap(data, map, false, false);
            table.addCell(new Cell().add(new Paragraph(count.toString())).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("name"))))
                    .setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("model")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("age")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("date")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(ObjectUtil.isNull(map.get("remark")) ? "" : String.valueOf(map.get("remark"))))
                    .setTextAlignment(TextAlignment.CENTER));
        }
        // 将表格添加到文档中
        document.add(table);

        // 关闭文档
        document.close();
    }

3.字符集下载

https://gitcode.com/open-source-toolkit/c17ea/overview

相关推荐
南风微微吹4 小时前
计算机二级MS Office历年真题试题及答案解析40套电子版PDF(含操作题和选择题)
pdf
小幂同学11 小时前
CAD图纸怎么转PDF和图片?CAD快速看图王(幂果)转换方法解析
pdf·cad
雾沉川11 小时前
WinRAR 压缩解压软件 功能介绍、硬件适配、横向对比及安装使用教程
windows·winrar·压缩软件
weixin_4692738113 小时前
银行流水 PDF 转 Excel 或者 CSV 完整指南
人工智能·pdf·excel
sukalot13 小时前
windows 驱动实例分析系列: wireguard-nt驱动分析-driver篇(一)
windows·驱动开发
百事牛科技14 小时前
Excel部分单元格禁止修改:锁住该锁的,放开该放的
windows·excel
水饺编程14 小时前
第5章,[Win32 章节] :创建、选择和删除画笔
c语言·c++·windows·visual studio
曹牧16 小时前
C#与Java后台交互
java·windows·microsoft·c#
꯭爿꯭巎꯭16 小时前
视频画质修复软件
windows
AI导出鸭17 小时前
腾讯ima的LaTeX生成PDF文件复制后数学公式乱码,怎样修改?AI导出鸭苹果版硬核拆解
人工智能·pdf·ai导出鸭