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

用freemarker

先做一个word模板  

将word 保存成 xml 打开xml找到对应的数据的地方 换成占位符  如:${city_feature_average} 都换好后 把xml 改成ftl

public class WordHelper {
private static final DebugPrn log = new DebugPrn(WordHelper.class.getName());

private String templateFileName;
private String generatedWordName;

WordHelper(String wordName){
this.templateFileName = wordName+".ftl";
this.generatedWordName = wordName+".doc";
}
/**
*方法功能:导出word文档
*参数:导出文档的路径; 文档输出的httpresponse
*返回值:
*抛出异常:
*/
public boolean ExportWord(String exportedWord, HttpServletResponse response) {
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;
}
/**
*方法功能:获得word模板,并将数据填入模板
*参数:传入模板的数据,map型,key对应模板占位符
*返回值:
*抛出异常:
*/
public 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(this.templateFileName);
} 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(WordHelper.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+this.generatedWordName;
}
}


public class ReportTaxi extends AbstractServletHandler {

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

}
}

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;
}
}

function ExprtMonthReport(){
}

ExprtMonthReport.prototype.downLoadWord = function(img_data){
var form=$("<form>");
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action","/ipeg-web/requestDispatcher");
var imgData=$("<input>");
imgData.attr("type","hidden");
imgData.attr("name","imgData");
imgData.attr("value",img_data);
var commandCode=$("<input>");
commandCode.attr("type","hidden");
commandCode.attr("name","commandCode");
commandCode.attr("value",19203110);
$("body").append(form);
form.append(imgData);
form.append(commandCode);
form.submit();
}