【IDP】自定义导出方法
前端操作
前端代码
添加导出按钮 代码如下
。
// An highlighted block
debugger;
//grid_596034 为列表页表格标识
var selected = idp.control.get('grid_596034').selected;
if (selected.length == 0) {
idp.warn("请选中一条要导出的数据!");
return false;
}
var dataid = selected[0].ID;
//style为表单标识,决定后端走哪个业务查询sql!
idp.service.fetch("/api/jk/jk/v1.0/exceltool/exceltool/excelExport",{dataid:dataid,style:"rkxx"},false).done(function (data) {
var fileName = data.fileName;
idp.exportFile(
//实现Excel下载构件
"com.inspur.gs.jk.exceltool.core.service.FileDownLoadService",
//自定义参数
{
fileName:fileName,
//downloadName为最终浏览器下载的文件名,需要自定义
downloadName:"入库信息.xlsx"
}
)
idp.tips("导出Excel成功");
}).fail(function (data) {
idp.error("导出Excel失败");
})
后端操作
后端代码路径为package com.inspur.gs.jk.exceltool.core.service;
后端主要操作为写查询业务数据的sql逻辑。
后端代码
业务数据查询
public Map excelExport(Map map) {
Map resultMap = new HashMap();
//获取前端表单标识style和业务数据ID
String style = map.get("style").toString();
String dataid = map.get("dataid").toString();
String fileName = null;
String mainSql;
String detailSql;
String name;
switch (style) {
case "rkxx"://判断表单标识 Mainsql为主表数据,detailsql为子表数据
mainSql = "SELECT * FROM JK_IOSTOCK WHERE JK_IOSTOCK.ID = ? ";
detailSql = "SELECT JK_MATERIALDETAIL.* FROM JK_MATERIALDETAIL WHERE JK_MATERIALDETAIL.PARENTID = ? ORDER BY JK_MATERIALDETAIL.MATERIALNO ";
name = "入库信息";//name为生成的excel文件名称
//detailExport为主子表导出,单独导主表调用mainExport
fileName = detailExport(dataid, mainSql, detailSql, name);
break;
default:
break;
}
resultMap.put("fileName", fileName);
return resultMap;
}
Excel文件生成
public String detailExport(String dataid, String mainSql, String detailSql, String name) {
String fileName;
try {
List mainList = Db.use().query(mainSql, dataid);//查询主表数据
List childList = Db.use().query(detailSql, dataid);//查询子表数据
String uuid = UUID.randomUUID().toString();
fileName = name + uuid + ".xlsx";//文件名拼接uuid生成唯一文件名
//本地测试需要修改filePath为 本地绝对路径
// String filePath = "C:\\Users\\liufu\\Desktop\\fsdownload\\" + fileName;
//当前filePath为生产路径,不要修改,需要测试就把生产路径注释,用上面的本地路径修改测试!
String filePath = "/usr/local/igix2103/igix2103/GSCloud2103/excelFiles/"+fileName;
//Excel输出 name为sheet1工作簿名称
ExcelWriter writer = ExcelUtil.getWriter(filePath, name);
//写出主表内容,输出标题
writer.write(mainList, true);
//切换sheet,此时从第0行开始写
writer.setSheet("明细数据");
//写出子表数据
writer.write(childList, true);
//关闭writer,释放内存
writer.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return fileName;
}
文件下载方法
不需要修改代码,测试只需要修改filePath路径
@InvokeMethod
public FileDownload excelDownload(Map<String, String> extendInfo) throws IOException {
String filePath = "/usr/local/igix2103/igix2103/GSCloud2103/excelFiles/";
// String filePath = "C:\\Users\\liufu\\Desktop\\fsdownload\\";
String fileName = extendInfo.get("fileName");
String downloadName = extendInfo.get("downloadName");
if (fileName == null || fileName.equals("")) {
String message = "文件名为空!";
log.error(message);
throw new ResultException(message);
}
File excelFile = new File(filePath + fileName);
if (excelFile.isFile() && excelFile.exists()) {
BufferedInputStream bufferInput = new BufferedInputStream(new FileInputStream(excelFile));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
byte[] bb = new byte[2048];
int ch = bufferInput.read(bb);
while (ch != -1) {
byteStream.write(bb, 0, ch);
ch = bufferInput.read(bb);
}
FileDownload file = new FileDownload();
file.fileData = byteStream.toByteArray();
file.fileName = downloadName;
return file;
} catch (Exception e) {
log.error(e.getMessage());
throw new ResultException(e.getMessage());
}finally {
bufferInput.close();
byteStream.close();
File delFile = new File(filePath + fileName);
if (delFile.exists()) {
delFile.delete();
System.out.println("文件"+filePath + fileName + "已删除!");
log.info("文件"+filePath + fileName + "已删除!");
}
}
}
log.error("文件"+filePath + fileName + "不存在!");
throw new ResultException("文件"+filePath + fileName + "不存在!");
}