生成表格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

相关推荐
二豆是富婆1 小时前
vue项目使用html2canvas和jspdf将页面导出成PDF文件
pdf
听到微笑3 小时前
使用ZSH美化Windows系统Git Bash
windows·git·bash
无尽星海max6 小时前
鲁大师绿色版,纯净无广告
windows·绿化
摸鱼佬在线11 小时前
基于关键字定位的自动化PDF合同拆分
python·pdf
苏小夕夕12 小时前
spark(三)
大数据·windows·spark
Doris Liu.12 小时前
使用Windows工具进行内存取证(不进行完全内存转储)
windows·内存取证·取证·电子数据取证·内存转储
虾球xz13 小时前
游戏引擎学习第213天
c++·windows·学习·游戏引擎
北京_宏哥13 小时前
🔥PC端自动化测试实战教程-3-pywinauto 启动PC端应用程序 - 下篇(详细教程)
前端·windows·python
pound12714 小时前
第五章.python函数
windows·python·microsoft
挣扎的蓝藻1 天前
使用 Python 扫描 Windows 下的 Wi-Fi 网络实例演示
网络·windows·python