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", "文件名称"); }
文章展示了如何在Java环境中使用SmartBiSDK来建立连接、设置参数并导出报表。首先,引入SmartBi的依赖,然后创建一个工具类SmartBiUtils,包含打开连接、导出报表的方法。在方法中,通过设置参数并调用SSReport接口导出Excel文件。最后,给出使用示例,展示如何注入SmartBiUtils并调用其方法来导出报表。
984

被折叠的 条评论
为什么被折叠?



