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

相关推荐
蚰蜒螟28 分钟前
深入 Linux 内核同步机制:从 futex 到 spinlock 的完整旅程
linux·windows·microsoft
dllmayday2 小时前
Linux 上用终端连接 WiFi
linux·服务器·windows
Muyuan19985 小时前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
Curtain_Gin8 小时前
windows nvim lazy
windows
生而为虫10 小时前
Claude Code 最新版安装教程(Windows/Mac/Linux 全平台) 面向普通用户的 Claude Code 安装与模型接入指南
linux·windows·macos
DevilSeagull10 小时前
Windows 批处理 (Batch) 编程: 从入门到入土. (一) 基础概念与环境配置
开发语言·windows·后端·batch·语言
CyL_Cly11 小时前
Parsec(parsec安卓/windows/macos下载)
windows
开开心心就好11 小时前
近200个工具的电脑故障修复合集
安全·智能手机·pdf·电脑·consul·memcache·1024程序员节
其实秋天的枫11 小时前
2026年初中英语大纲词汇表1600词
经验分享·pdf
2601_9583205712 小时前
【小白零基础上手 】钉钉内部机器人接入 OpenClaw 完整流程讲解(含安装包)
人工智能·windows·机器人·钉钉·open claw·open claw安装