poi生成带图片word

//Blank Document
        XWPFDocument document = new XWPFDocument();
        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File("C:\\Users\\cYS2646\\Desktop\\POI图片文件生产.docx"));
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("Java PoI");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
        //段落
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("Java POI 生成word文件。");
        run.setColor("696969");
        run.setFontSize(16);
        //设置段落背景颜色
        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
        cTShd.setVal(STShd.CLEAR);
        cTShd.setFill("97FFFF");
        //换行
        XWPFParagraph paragraph1 = document.createParagraph();
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setText("\r");
        //基本信息表格
        XWPFTable infoTable = document.createTable();
        //去表格边框
        infoTable.getCTTbl().getTblPr().unsetTblBorders();
        //列宽自动分割
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
        //表格第一行
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("职位");
        infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");
        //表格第二行
        XWPFTableRow infoTableRowTwo = infoTable.createRow();
        infoTableRowTwo.getCell(0).setText("姓名");
        infoTableRowTwo.getCell(1).setText(": seawater");
        //表格第三行
        XWPFTableRow infoTableRowThree = infoTable.createRow();
        infoTableRowThree.getCell(0).setText("生日");
        infoTableRowThree.getCell(1).setText(": xxx-xx-xx");
        //表格第四行
        XWPFTableRow infoTableRowFour = infoTable.createRow();
        infoTableRowFour.getCell(0).setText("性别");
        infoTableRowFour.getCell(1).setText(": 男");
        //表格第五行
        XWPFTableRow infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("现居地");
        infoTableRowFive.getCell(1).setText(": xx");
        //两个表格之间加个换行
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
        //工作经历表格
        XWPFTable ComTable = document.createTable();
        //列宽自动分割
        CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
        //表格第一行
        XWPFTableRow comTableRowOne = ComTable.getRow(0);
        comTableRowOne.getCell(0).setText("开始时间");
        comTableRowOne.addNewTableCell().setText("结束时间");
        comTableRowOne.addNewTableCell().setText("公司名称");
        comTableRowOne.addNewTableCell().setText("title");
        //表格第二行
        XWPFTableRow comTableRowTwo = ComTable.createRow();
        comTableRowTwo.getCell(0).setText("2016-09-06");
        comTableRowTwo.getCell(1).setText("至今");
        comTableRowTwo.getCell(2).setText("seawater");
        comTableRowTwo.getCell(3).setText("Java开发工程师");
        //表格第三行
        XWPFTableRow comTableRowThree = ComTable.createRow();
        comTableRowThree.getCell(0).setText("2016-09-06");
        comTableRowThree.getCell(1).setText("至今");
        comTableRowThree.getCell(2).setText("seawater");
        XWPFTableCell xwpfTableCell3 =  comTableRowThree.getCell(3);
        //给cell加一张图片
        XWPFParagraph p = xwpfTableCell3.getParagraphArray(2);
        if (p == null) {
            p = xwpfTableCell3.addParagraph();
        }
        p.setAlignment(ParagraphAlignment.CENTER);
        p.setVerticalAlignment(TextAlignment.CENTER);
        XWPFRun pic = p.createRun();
        byte[] fileByteArray = this.restTemplateService.getFileByteArray("http://pic.url.com/file/down?file=130", new HttpHeaders());
        try {
            pic.addPicture(new ByteArrayInputStream(fileByteArray), Document.PICTURE_TYPE_PNG, "123.png", Units.toEMU(20), Units.toEMU(20));
        } catch (Exception e) {
            e.printStackTrace();
        }
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
        //添加页眉
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "Java POI create MS word file.";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //设置为右对齐
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
        //添加页脚
        CTP ctpFooter = CTP.Factory.newInstance();
        CTR ctrFooter = ctpFooter.addNewR();
        CTText ctFooter = ctrFooter.addNewT();
        String footerText = "http://blog.csdn.net/zhouseawater";
        ctFooter.setStringValue(footerText);
        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
        parsFooter[0] = footerParagraph;
        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
        document.write(out);
        out.close();
        System.out.println("create_table document written success.");
         <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>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用Freemarker和POI结合生成水印的Word文档。下面是一个简单的示例: 1. 首先,需要准备一个模板Word文档,其中可以包含一些占位符,例如`${title}`和`${content}`,表示需要根据数据填充的部分。 2. 使用POI读取模板Word文档,并将其转换为`XWPFDocument`对象。 3. 使用Freemarker生成需要填充的数据,并将其保存在一个Map中。 4. 使用Freemarker将数据填充到模板中,生成一个新的Word文档。 5. 在新生成Word文档中添加水印。 下面是示例代码: ```java // 读取模板Word文档 XWPFDocument doc = new XWPFDocument(new FileInputStream("template.docx")); // 准备数据 Map<String, Object> dataMap = new HashMap<>(); dataMap.put("title", "这是标题"); dataMap.put("content", "这是内容"); // 使用Freemarker填充数据 Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); configuration.setDefaultEncoding("UTF-8"); Template template = configuration.getTemplate("template.ftl"); StringWriter writer = new StringWriter(); template.process(dataMap, writer); // 将填充后的内容写入新的Word文档 XWPFDocument newDoc = new XWPFDocument(new ByteArrayInputStream(writer.toString().getBytes())); FileOutputStream out = new FileOutputStream("output.docx"); newDoc.write(out); out.close(); // 添加水印 XWPFParagraph waterMarkParagraph = doc.createParagraph(); waterMarkParagraph.createRun().setText("水印内容"); waterMarkParagraph.setSpacingAfter(0); waterMarkParagraph.setSpacingBefore(0); waterMarkParagraph.setAlignment(ParagraphAlignment.CENTER); waterMarkParagraph.setVerticalAlignment(TextAlignment.CENTER); CTTcPr tcPr = waterMarkParagraph.getCTP().addNewR().addNewRPr().addNewTcPr(); tcPr.addNewTcW().setW(BigInteger.valueOf(10000)); tcPr.addNewVAlign().setVal(STVerticalJc.CENTER); waterMarkParagraph.getCTP().addNewPPr().addNewJc().setVal(STJc.CENTER); for (XWPFParagraph paragraph : newDoc.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { run.getCTR().addNewRPr().addNewNoProof(); } } newDoc.write(out); out.close(); ``` 注意,这只是一个简单的示例,实际上需要根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值