POI-TL:Word嵌套导致图表标题不生效,图表内容未无法刷新问题简易解决方案
作者说是BUG,新版本不知道有没有修复
复现问题
参考 https://github.com/Sayi/poi-tl/issues/649 时间比较赶(马上需求DDL了,抽空速度记录一下)
issue里面说的很明白,也有大佬给出了解决方案,不过要重新打包,太麻烦了
我这里就搞了个简单点的,你就说能不能用吧
你需要会的东西
http://deepoove.com/poi-tl/#_%E5%B5%8C%E5%A5%97 POI-TL 嵌套
http://deepoove.com/poi-tl/#_%E5%BC%95%E7%94%A8%E6%A0%87%E7%AD%BE POI-TL 图表
解决方案
简单描述就是:
直接拿到NiceXWPFDocument
对象进行write
(一定要write,BUG就是数据没有写进图表里面)转成流再用DocxRenderData
对象接收
代码如下:
public class demo {
public static void main(String[] args) throws Exception {
// sub ----------------------------------------------------------------
//饼状图数据
ChartSingleSeriesRenderData underManageAssetChart = Charts.ofSingleSeries("", new String[]{"主机设备","网络设备","合并"}).series("countries", new Integer[]{10, 20, 30}).create();
//子模板数据
Map<String, Object> subData = new HashMap<>();
subData.put("chart",underManageAssetChart);
//这里直接得到NiceXWPFDocument对象
NiceXWPFDocument sub = XWPFTemplate.compile("E:\\workspace\\src\\main\\resources\\doc\\sub_template.docx").render(subData).getXWPFDocument();
//用流写出来
ByteArrayOutputStream stream = new ByteArrayOutputStream();
sub.write(stream);
//然后创建DocxRenderData对象,这个就是用来嵌套的word数据对象
DocxRenderData subDocx = new DocxRenderData(new ByteArrayInputStream(stream.toByteArray()));
// sub ----------------------------------------------------------------
//这里就和官方实例一样就行了
Map<String, Object> data = new HashMap<>();
//加入主模板
data.put("var", "见证奇迹捏");
data.put("chart", subDocx);
//主模板
XWPFTemplate template = XWPFTemplate.compile("src/main/resources/template/test.docx").render(data);
//生成文件
template.writeAndClose(new FileOutputStream("result.docx"));
}
}