jeecgboot/JeecgBoot · error · RuntimeException

生成Word模版失败: {message}

Error message

生成Word模版失败: {message}

What it means

WordTplUtils.generateWordTemplate() builds an XWPFDocument (header/footer/body rendering) then writes it to the supplied outputStream. The doc.write/flush/close is wrapped in a try that rethrows any IOException as a RuntimeException carrying the cause message.

Source

Thrown at jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/wordtpl/utils/WordTplUtils.java:91

        WordUtil.setPaperMargins(doc, margins);

        // TODO author: chenrui for:水印设置 date:2025/7/4

        // 渲染页眉和页脚
        renderHeaderAndFooter(template, doc);

        // 文档主体渲染 date:2025/7/4
        renderDocumentBody(doc, template);

        // 页码
//        addPageNumbers(doc, 0);

        try {
            doc.write(outputStream);
            outputStream.flush();
            doc.close();
        } catch (Exception e) {
            throw new RuntimeException("生成Word模版失败: " + e.getMessage(), e);
        }
    }

    /**
     * 渲染页眉和页脚
     *
     * @param template
     * @param doc
     * @author chenrui
     * @date 2025/7/10 17:52
     */
    private static void renderHeaderAndFooter(AigcWordTemplate template, XWPFDocument doc) {
        //页眉
        JSONArray header = JSON.parseArray(template.getHeader());
        if (oConvertUtils.isObjectNotEmpty(header)) {
            XWPFHeader docHeader = doc.createHeader(HeaderFooterType.DEFAULT);
            XWPFParagraph paragraph = null;
            for (int i = 0; i < header.size(); i++) {

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Inspect the cause message in the RuntimeException.
  2. Validate the AigcWordTemplate fields and any image references before generation.
  3. Ensure the passed outputStream is open, non-null, and not shared.
  4. If OOM, raise heap or reduce document size.
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the output stream is open and writable before generation
if (outputStream == null) { throw new IllegalArgumentException("outputStream 为空"); }

Try / catch

try (XWPFDocument doc = new XWPFDocument()) {
    renderHeaderAndFooter(template, doc);
    renderDocumentBody(doc, template);
    doc.write(outputStream);
    outputStream.flush();
} catch (Exception e) {
    throw new RuntimeException("生成Word模版失败: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: The document structure is internally inconsistent (bad relation, broken image reference); the outputStream is closed/null; an OutOfMemoryError-scaled doc; POI throws on an unsupported style/setting.

Common situations: Caller closed the stream before write; template definition references an image that failed to load; concurrent reuse of the same XWPFDocument; very large document exhausting memory.

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/7647d1618e3b6509. Report an issue: GitHub.