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
- Inspect the cause message in the RuntimeException.
- Validate the AigcWordTemplate fields and any image references before generation.
- Ensure the passed outputStream is open, non-null, and not shared.
- 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
- Do not close the outputStream from inside generateWordTemplate (caller owns it).
- Validate template image references before building the doc.
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
- 下载word模版失败: {message}
- 生成word文档失败: {message}
- 添加图片异常: {message}
- 解析word模版失败: {message}
- 生成word文档失败,请检查模版和数据是否正确
AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14).
Data as JSON: /api/errors/7647d1618e3b6509.
Report an issue: GitHub.