• 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();
    }


  • 相关阅读:
    查看文件(或文件夹)被哪个进程使用【文件已在另一程序中打开】
    MinGW32和64位交叉编译环境的安装和使用
    MinGW下编译openssl, json-c
    曲演杂坛--SQLCMD下执行命令失败但没有任何错误提示的坑
    Python2.6下基于rsa的加密解密
    MySQL--使用xtrabackup进行备份还原
    Python--过滤Mysql慢日志
    MySQL--将MySQL数据导入到SQL Server
    Python--命令行参数解析Demo
    python--同一mysql数据库下批量迁移数据
  • 原文地址:https://www.cnblogs.com/hyp5490-/p/5541278.html
Copyright © 2020-2023  润新知