0


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>

2.java代码示例

publicclass test {publicstaticvoidmain(String[] args)throwsIOException,InvalidFormatException{

        log.info("Word文档开始生成!");XWPFDocument document =newXWPFDocument();WriteHomePage(document,1,"SSS");//写首页WriteText(document);//写表格前正文与题注WriteFirstTable(document);//写第一个表格FileOutputStream out =newFileOutputStream("Report.docx");
        document.write(out);
        out.close();
        document.close();
        log.info("Word文档已成功生成!");}publicstaticXWPFDocumentWriteHomePage(XWPFDocument document,int orb,String salliteName)throwsIOException,InvalidFormatException{//插入图片String imgFile ="E:\\test.png";byte[] imageBytes =Files.readAllBytes(newFile(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(newByteArrayInputStream(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;}publicstaticXWPFDocumentWriteText(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;}//输入表格publicstaticXWPFDocumentWriteFirstTable(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.效果展示
在这里插入图片描述

标签: java word导出

本文转载自: https://blog.csdn.net/lz20120808/article/details/133378921
版权归原作者 坚持奋斗的李洛克 所有, 如有侵权,请联系我们删除。

“java导出word(含图片、表格)”的评论:

还没有评论