echarts 图形 导出到后台 word 图片加文字

Posted 路过的二师兄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了echarts 图形 导出到后台 word 图片加文字相关的知识,希望对你有一定的参考价值。

用freemarker

先做一个word模板 

public class ReportExporter extends AbstractServletHandler {
private static final DebugPrn log = new DebugPrn(ReportExporter.class.getName());

@Override
public String response(RequestParameters requestParameters) throws IPEGException {
initParameters(requestParameters);
String imgData = requestParameters.getRequest().getParameter("imgData").split(",")[1];
String exportedWord = createWord(WordData.getDataMap(imgData));
if(!"".equals(exportedWord)){
if(!ExportWord(exportedWord)){
return JsonHandlerUtils.faliureMessage;
}
}
return JsonHandlerUtils.successMessage;
}

private boolean ExportWord(String exportedWord) {
OutputStream toClient = null;
InputStream fis = null;
File wordFile = null;
try {
wordFile = new File(exportedWord);
String fileName = URLEncoder.encode(wordFile.getName(),"UTF-8");
if(fileName.length() > 150){
fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");
}
fis = new BufferedInputStream(new FileInputStream(wordFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
toClient = new BufferedOutputStream(response.getOutputStream());
response.reset();
response.setContentType("application/msword");
response.addHeader("Content-Disposition",
"attachment; filename=" + fileName);
response.addHeader("Content-Length",String.valueOf(wordFile.length()));
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
log.error("export word failed"+e.getMessage(), e);
return false;
}finally {
IOCloseUtil.close(fis);
IOCloseUtil.close(toClient);
wordFile.delete();
}
return true;
}

private String createWord(Map<String,Object> dataMap){
Configuration configuration = null;
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
Template t = null;
try {
configuration.setDirectoryForTemplateLoading(new File(getTemplatePath()));
t = configuration.getTemplate("raplace_word.ftl");
} catch (IOException e) {
log.error("load template failed!"+e.getMessage(), e);
return "";
}
File outFile = new File(getWordPath());
Writer out = null;
FileOutputStream fos=null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");
out = new BufferedWriter(oWriter);
t.process(dataMap, out);
out.flush();
} catch (Exception e) {
log.error("report write word stream failed"+ e.getMessage(), e);
return "";
}finally {
IOCloseUtil.close(out);
IOCloseUtil.close(fos);
}
return getWordPath();
}

private String getTemplatePath(){
SystemSupportService smService = ServiceAccess.getSystemSupportService();
Par par = null;
try {
par = smService.getDeployedPar(ReportExporter.class);
} catch (SystemSupportException e) {
log.error("get Par‘s path failed!"+e.getMessage(), e);
}
return par.getBaseDir() +File.separator+ "conf"+File.separator+"template";
}

private String getWordPath(){
return getTemplatePath()+File.separator+"replaceWord.doc";
}
}

public class WordData {
public static Map<String, Object> getDataMap(String imgData) {
String img_data = imgData.replace(‘ ‘,‘+‘);
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("city_feature_average", "dfdfdfdf");
dataMap.put("city_feature_level", "2222");
dataMap.put("city_feature_YoY_up_down", "33333");
dataMap.put("city_feature_MoM_up_down", "4444");
dataMap.put("city_feature_image", img_data);
return dataMap;
}
}



以上是关于echarts 图形 导出到后台 word 图片加文字的主要内容,如果未能解决你的问题,请参考以下文章

如何在echarts 上面自定义图形

word文字怎么转到ae里面

数学书上的图片如何导出

JS如何导出Div的内容为图片

利用jQuery-Word-Export导出word (含表格,ECharts)

java web实现在线编辑word,并将word导出