下载smartBi报表

1、导包 

 

<dependency>
    <groupId>net.herdao.smartbi</groupId>
    <artifactId>SDK</artifactId>
    <version>1.0.0</version>
</dependency>

2、实现smartBi工具类

import smartbi.sdk.ClientConnector;
import smartbi.sdk.service.spreadsheetreport.SSReport;

@Component
public class SmartBiUtils {

    //获取SmartBi链接
    public ClientConnector openConn(){
        String url = "http://ip地址:8888/smartbi";
        String user = "账号";
        String password = "密码";
        ClientConnector connector = new ClientConnector(url);
        boolean bool = connector.open(user, password);
        return bool ? connector : null;
    }

   /**
    * 导出smartBI报表
    * params:获取报表的参数
    * redisId:报表ID
    * smartBIName:下载的文件名称
    */
    public String exportSmartBIByCode(List<JSONObject> params,
                                      String redisId,
                                      String smartBIName) throws Exception {
        ClientConnector connector = openConn();
        if(null == connector){
            return null;
        }
        FileOutputStream fileOutputStream = null;
        try {
            //链接smartBi
            SSReport report = new SSReport(connector);
            //打开报表
            report.open(redisId);
           
            //有参数时设置参数,参数id是数据集的输出参数id
            for (JSONObject param : params) {
                report.setParamValue(param.get("name").toString(),param.get("value").toString(),param.get("displayValue").toString());
            }
            //定义下载文件的本地地址
            String filePath = "static/"+DateUtils.getFormatDate2Str("yyyyMMdd");
            File tempFile = new File(filePath);
            if(!tempFile.exists()){
                tempFile.mkdirs();
            }
            //定义文件的名称,若路径已存在该名称文件,则直接返回
            String fileName = filePath+"/"+smartBIName+".xlsx";
            File returnFile = new File(fileName);
            if(returnFile.exists()){
                return fileName;
            }
            fileOutputStream = new FileOutputStream(returnFile);
            report.doExport("EXCEL", fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            return fileName;
        } catch (IOException e) {
            log.error("bi报表获取出错:"+e.getMessage());
            e.printStackTrace();
            return null;
        }finally{
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException oute) {
                    oute.printStackTrace();
                }
            }
            if (connector != null) {
                try {
                    connector.close();
                } catch (Exception ine) {
                    ine.printStackTrace();
                }
            }
        }
    }

    //封装smartbi参数
    public static JSONObject encapObject(String name,String value,String displayValue){
        JSONObject json = new JSONObject();
        json.put("name",name);
        json.put("value",value);
        json.put("displayValue",displayValue);
        return json;
    }

}

3、使用示例

@Autowired

private SmartBiUtils smartBiUtils;

public void exportSmartBI(){

   List<JSONObject> objectList = new ArrayList<>();
   objectList.add(setJsonObject("OutputParameter.I8a8192120178aa8aaa8a3625.月份","202304","2023-04"));
   objectList.add(setJsonObject("OutputParameter.I8a8192120178aa8aaa8a3625.项目名称","xiangmubianhao","xiangmumingcheng"));
   smartBiUtils.exportSmartBIByCode(objectList, "报表ID", "文件名称");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值