Echarts图表Java后端生成请看上篇,此篇为Base64图片插入Word文档中
Java后台生成ECharts图片,并以Base64字符串返回_青冘的博客-CSDN博客
java
try {
XWPFParagraph xwpfParagraphimage = doc.createParagraph(); // 创建图片段落
xwpfParagraphimage.setAlignment(ParagraphAlignment.CENTER);
XWPFRun xwpfRunimage = xwpfParagraphimage.createRun(); // 创建段落文本
String jsonData = echartsOption.getJsonDataZCRLQK();//获取OPT的Json配置
EchartData echartData = new Gson().fromJson(EchartData2Base64.echartToBase64(jsonData), EchartData.class);
if (echartData.getCode() == 1) {
// 依赖包sun.misc.BASE64Decoder
BASE64Decoder decoder = new BASE64Decoder();
byte[] decoderBytes = decoder.decodeBuffer(echartData.getData());
InputStream imageis = new ByteArrayInputStream(decoderBytes);
BufferedImage imagebi = ImageIO.read(new ByteArrayInputStream(decoderBytes));
xwpfRunimage.addPicture(imageis, Document.PICTURE_TYPE_PNG, "",
Units.pixelToEMU(Math.min(imagebi.getWidth(), 360)), Units.pixelToEMU(Math.min(imagebi.getHeight(), 240)));
}
} catch (Exception e) {
throw new RuntimeException(e);
}