• Java使用apache.poi生成excel插入word中


    加油,新时代打工人!

    工作需求,上个文章我们生成好的word,这次将生成好的excel表格数据,插入word中。需要准备好excle数据,然后插入到word中。
    最后个需要,就是把这些生成好的word文档转成pdf进行前端下载下来。
    Java使用apache.poi生成word

    在这里插入图片描述

    package com.wh.filedownload.controller;
    
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableCell;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * @author wh
     * @date 2024年05月22日16:12
     * 将excel表格数据,导入现有的word模板中.
     */
    public class word2 {
        public static void main(String[] args) throws IOException, InvalidFormatException {
    
            String excelFilePath = "sample.xlsx";
            String wordFilePath = "output.docx";
            String tempWordFilePath = "tempWordFile.docx";
    
            FileInputStream excelFileInputStream = new FileInputStream(new File(excelFilePath));
            Workbook workbook = WorkbookFactory.create(excelFileInputStream);
            Sheet sheet = workbook.getSheetAt(0); // 假设只处理第一个sheet
    
            FileInputStream wordFileInputStream = new FileInputStream(new File(wordFilePath));
            XWPFDocument document = new XWPFDocument(wordFileInputStream);
            int rowCount = sheet.getLastRowNum();
            int colCount = sheet.getRow(0).getLastCellNum(); // 假设第一行有最多的列
            // 创建Word中的表格
            XWPFTable table = document.createTable(rowCount + 1, colCount); // +1行是为了标题
            // 获取或创建表格的边框设置对象
            CTTblBorders tblBorders = table.getCTTbl().getTblPr().getTblBorders();
    
            // 设置表格边框样式
            tblBorders.getTop().setVal(STBorder.SINGLE); // 上边框
            tblBorders.getLeft().setVal(STBorder.SINGLE); // 左边框
            tblBorders.getRight().setVal(STBorder.SINGLE); // 右边框
            tblBorders.getBottom().setVal(STBorder.SINGLE); // 下边框
    
            // 填充表格标题
            Row headerRow = sheet.getRow(0);
            for (int i = 0; i < colCount; i++) {
                XWPFTableCell cell = table.getRow(0).getCell(i);
                cell.setText(headerRow.getCell(i).getStringCellValue());
            }
    
            // 填充表格数据
            for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++) { // 从1开始,跳过标题行
                Row dataRow = sheet.getRow(rowIndex);
                if (dataRow == null) continue; // 跳过空行
                XWPFTableRow tableRow = table.getRow(rowIndex); // 对应的Word表格行
    
                for (int colIndex = 0; colIndex < colCount; colIndex++) {
                    Cell cell = dataRow.getCell(colIndex);
                    if (cell == null) continue; // 如果单元格为空,则跳过
    
                    switch (cell.getCellType()) {
                        case STRING:
                            tableRow.getCell(colIndex).setText(cell.getStringCellValue());
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                tableRow.getCell(colIndex).setText(cell.getLocalDateTimeCellValue().toString());
                            } else {
                                tableRow.getCell(colIndex).setText(String.valueOf(cell.getNumericCellValue()));
                            }
                            break;
                        // 可以继续添加对其他类型的处理
                    }
                }
            }
    
            wordFileInputStream.close();
            FileOutputStream out = new FileOutputStream(tempWordFilePath);
            document.write(out);
            out.close();
            document.close();
            workbook.close();
            excelFileInputStream.close();
    
            // 这里可以选择将tempWordFilePath移动或重命名为wordFilePath以覆盖原文件
            // 注意:这一步骤会永久改变原Word文档
                 File srcFile = new File(tempWordFilePath);
                 File destFile = new File(wordFilePath);
                 srcFile.renameTo(destFile);
    
            System.out.println("Excel content has been appended to the Word document.");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    运行结果。
    在这里插入图片描述

  • 相关阅读:
    Avanci与现代汽车和起亚签署专利许可协议
    Java面试总结(2021优化版)发布&1024程序员节
    该选择WPF 还是 Winform?
    实战演练 | 在 MySQL 中选择除了某一列以外的所有列
    ASO优化如何做?这几个技巧你绝对要了解
    选择合适的 DevOps 工具,从理解 DevOps 开始
    每天5分钟快速玩转机器学习算法:带有核函数的支持向量机模型
    云上办公兴起,华为云桌面Workspace更靠谱
    【element-ui】 el-form之rules赋值后校验没消失
    Vxlan协议原理及基本配置——网络测试仪实操手册
  • 原文地址:https://blog.csdn.net/weixin_45397785/article/details/139164753