• java导出word(含图片、表格)


    1.pom 引入

     <!--word报告生成依赖-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.2</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.java代码示例

    
    public class test {
        public static void main(String[] args) throws IOException, InvalidFormatException {
    
            log.info("Word文档开始生成!");
            XWPFDocument document = new XWPFDocument();
            WriteHomePage(document,1,"SSS"); //写首页
            WriteText(document); //写表格前正文与题注
            WriteFirstTable(document); //写第一个表格
    
            FileOutputStream out = new FileOutputStream("Report.docx");
            document.write(out);
            out.close();
            document.close();
            log.info("Word文档已成功生成!");
        }
        public static XWPFDocument WriteHomePage(XWPFDocument document, int orb, String salliteName) throws IOException, InvalidFormatException {
    
            //插入图片
            String imgFile = "E:\\test.png";
            byte[] imageBytes = Files.readAllBytes(new File(imgFile).toPath());
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run00 = paragraph.createRun();
            run00.setText(" ");
            int width = 200; // 图片宽度
            int height = 140; // 图片高度
            int pictureType = XWPFDocument.PICTURE_TYPE_PNG;
            run00.addPicture(new ByteArrayInputStream(imageBytes), pictureType, imgFile, Units.toEMU(width), Units.toEMU(height));
            //输入标题
            XWPFParagraph title1 = document.createParagraph();
            XWPFRun run = title1.createRun();
            run.setText("O00");
            run.setUnderline(UnderlinePatterns.SINGLE);
            run.setFontFamily("Times New Roman");
            run.setFontSize(28);
            run.setBold(true);
            run.setItalic(true);
            //不同字体设置的参数大小不同,需要尝试
            title1.setIndentationFirstLine(1130);// 
            run.addBreak();
            XWPFParagraph title2 = document.createParagraph();
            XWPFRun run1 = title2.createRun();
            run1.setText(salliteName+" Continuity of Data");
            run1.setFontFamily("Times New Roman");
            run1.setFontSize(28);
            run1.setBold(true);
            title2.setIndentationFirstLine(1130); // 
            //设置空白行
            for (int i = 0; i < 16; i++) {
                XWPFParagraph blank = document.createParagraph();
                blank.createRun().setText("");
            }
            // 创建段落并设置右对齐样式
            XWPFParagraph footer = document.createParagraph();
            footer.setAlignment(ParagraphAlignment.RIGHT);
            // 创建页脚并添加到段落中
            XWPFRun run2 = footer.createRun();
            run2.setText("Data Center");
            run2.setFontSize(18);
            run2.setFontFamily("Times New Roman");
            run2.addBreak();
            XWPFRun run3 = footer.createRun();
            run3.setText("www.xxx.com");
            run3.setFontSize(18);
            run3.setFontFamily("Times New Roman");
            run3.setUnderline(UnderlinePatterns.SINGLE);
            run3.addBreak();
            return document;
        }
    
        public static XWPFDocument WriteText(XWPFDocument document){
            //新建一页,创建表格
            XWPFParagraph newPage = document.createParagraph();
            newPage.setPageBreak(true);
            XWPFParagraph para = document.createParagraph();
            XWPFRun run = para.createRun();
            run.setText("该报告数据内容正文如下。");
            run.setFontFamily("宋体");
            run.setFontSize(12);//小四
    //        XWPFFont font =  ;
            para.setIndentationFirstLine(500); // 
            para.setSpacingBetween(1.5f);
            //表题注
            XWPFParagraph para2 = document.createParagraph();
            XWPFRun runPara2 = para2.createRun();
            runPara2.setText("表1  数据概况");
            runPara2.setFontFamily("宋体");
            runPara2.setFontSize(12);
            para2.setAlignment(ParagraphAlignment.CENTER);
            para2.setVerticalAlignment(TextAlignment.CENTER);
            para2.setSpacingBetween(1.5f);
            return document;
        }
    
        //输入表格
        public static XWPFDocument WriteFirstTable(XWPFDocument document){
            int row = 2;
            int col = 2;
            XWPFTable table = document.createTable(row, col);
            // 设置表格行高
            for (XWPFTableRow tableRow : table.getRows()) {
                tableRow.setHeight(500);
            }
            table.setStyleID("Table Grid");
            table.setWidth("100%");
            //设置表格标题
            String[] titleArr = {"数据类型","数据连续性"};
            XWPFTableRow titleRow = table.getRow(0);
            //设置表头内容和样式,
            for (int i = 0; i < col; i++) {
                XWPFTableCell cell = titleRow.getCell(i);
                cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                XWPFParagraph p = cell.getParagraphs().get(0);
                p.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun run3 = p.createRun();
                run3.setBold(true); // 设置字体加粗
                run3.setText(titleArr[i]);
            }
            //CTHMerge 横向合并,CTVMerge纵向合并, STMerge.RESTART 表示开始合并,为 STMerge.CONTINUE 表示继续合并
    //        titleRow.getCell(4).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
    //        titleRow.getCell(5).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
    //        XWPFParagraph p = titleRow.getCell(4).getParagraphs().get(0);
    //        p.setAlignment(ParagraphAlignment.CENTER);
    
            XWPFTableRow row1 = table.getRow(1);
            row1.getCell(0).setText("11");
            row1.getCell(1).setText("12");
            XWPFTableRow row2 = table.getRow(1);
            row2.getCell(0).setText("21");
            row2.getCell(1).setText("22");
    
            return 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
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    3.效果展示
    在这里插入图片描述

  • 相关阅读:
    STM32F0单片机基于Hal库温控智能风扇
    决策中心:构建企业长期战略竞争力
    2173. 最多连胜的次数
    分析逆向案例九——奥鹏教育教师登录密码加密
    春风吹又生的开源项目「GitHub 热点速览」
    Sendable 和 @Sendable 闭包 —— 代码实例详解
    springboot常见网络相关错误及原因解析
    【吴恩达笔记】机器学习基础
    第2关:伪分布式体验及分布式安装配置
    MLOps的演进历程
  • 原文地址:https://blog.csdn.net/lz20120808/article/details/133378921