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

1.pom 引入

bash 复制代码
 <!--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>

2.java代码示例

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);// <!-- 设置首行缩进2字符 单位是 1/20 磅 -->
        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); // <!-- 设置首行缩进2字符 -->
        //设置空白行
        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); // <!-- 设置首行缩进2字符 -->
        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;
    }
}

3.效果展示

相关推荐
Be_Somebody2 分钟前
[这可能是最好的Spring教程!]Maven的模块管理——如何拆分大项目并且用parent继承保证代码的简介性
java·spring boot·spring·spring入门
一个数据小开发18 分钟前
业务开发问题之ConcurrentHashMap
java·开发语言·高并发·map
会飞的架狗师34 分钟前
【Spring】Spring框架中有有哪些常见的设计模式
java·spring·设计模式
Jakarta EE44 分钟前
在JPA和EJB中用乐观锁解决并发问题
java
花心蝴蝶.1 小时前
并发编程中常见的锁策略
java·jvm·windows
A_cot1 小时前
一篇Spring Boot 笔记
java·spring boot·笔记·后端·mysql·spring·maven
tryCbest2 小时前
java8之Stream流
java·后端
江梦寻3 小时前
解决SLF4J: Class path contains multiple SLF4J bindings问题
java·开发语言·spring boot·后端·spring·intellij-idea·idea
鸡鸭扣3 小时前
springboot苍穹外卖实战:五、公共字段自动填充(aop切面实现)+新增菜品功能+oss
java·spring boot·后端
徐*红5 小时前
java 线程池
java·开发语言