码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-30 09:57:16      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:

用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 图片加文字

标签:

原文地址:http://www.cnblogs.com/hyp5490-/p/5541278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!