echatrs html导出word,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 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 getDataMap(String imgData) {

String img_data = imgData.replace(‘ ‘,‘+‘);

Map dataMap = new HashMap();

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值