Java导出word 带附件功能

最近做得一个导出Word功能带附件,查得官网,免费贡献给需要得人。那些要收费得就不要了。

采用得版本是poi-tl 1.10.4,低版本得目前不支持。

 只需要加这端代码逻辑,即可导出,支持本地文件,文件流

AttachmentRenderData attach = Attachments.ofStream(this.getClass().getClassLoader().getResourceAsStream("model/计算书.docx"), AttachmentType.DOCX).create();
            calcReportArgMap.put("atta",attach);
            Configure configure = Configure.builder()
                    .bind("atta", new AttachmentRenderPolicy())
                    .build();
// 本地模板测试 导出
 XWPFTemplate template = XWPFTemplate.compile(this.getClass().getClassLoader().getResourceAsStream("model/计算书.docx.docx"),configure).render(calcReportArgMap);

 word模板采用普通标签{{atta}} 

导出效果如图:

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java导出水印的Word文档,可以使用Apache POI库来创建和编辑Word文档。下面是一个简单的示例代码: ```java import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.PositionOffset; import org.apache.poi.xwpf.usermodel.VerticalAlign; import org.apache.poi.xwpf.usermodel.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.usermodel.XWPFHeader; import org.apache.poi.xwpf.usermodel.XWPFWatermark; public class WordDocumentExporter { public static void exportDocument(String fileName) { try { XWPFDocument document = new XWPFDocument(); // Add content to the document XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("This is a sample text."); // Add watermark to the document XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy(); if (policy == null) { policy = document.createHeaderFooterPolicy(); } XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph watermark = header.createParagraph(); watermark.setAlignment(ParagraphAlignment.CENTER); watermark.setVerticalAlignment(TextAlignment.CENTER); XWPFRun watermarkRun = watermark.createRun(); watermarkRun.setText("CONFIDENTIAL"); watermarkRun.setFontSize(72); watermarkRun.setColor("000000"); watermarkRun.setBold(true); watermarkRun.setItalic(true); CTBackground background = watermark.getCTP().addNewBackground(); CTShd shd = background.addNewShd(); shd.setFill("FFFFFF"); shd.setColor("FFFFFF"); shd.setVal(STShd.CLEAR); // Save the document FileOutputStream out = new FileOutputStream(fileName); document.write(out); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个XWPFDocument对象,然后添加了一些文本内容。接下来,我们使用XWPFHeaderFooterPolicy和XWPFHeader类来创建文档的页眉,并在其中添加水印。在这个例子中,我们添加了一个“CONFIDENTIAL”水印,但你可以根据需要修改水印的文本和样式。最后,我们将文档保存到指定的文件中。 需要注意的是,导出水印的Word文档需要使用POI的最新版本,因为早期版本可能无法正确处理水印。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值