页面
前端
只记录核心点
const exportData = () => {
// Object.keys遍历每个key
const exportPara = Object.keys(paramsQuery)
.filter((k) => !!paramsQuery[k])//判断是否为真的,0 和空 null undefined 都为false
// encodeURIComponent 编码
// _.isObject 判断是否是对象,如果是对象,返回true
// _.isArray 判断是否是数组
// 如果是对象或者数组就转化为json
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(_.isObject(paramsQuery[k]) || _.isArray(paramsQuery[k]) ? JSON.stringify(paramsQuery[k]) : paramsQuery[k])}`)
.join('&')
// 必须要携带token
window.open(baseURL + '/third/PingGu/exportPingGuCallDetail?' + exportPara + '&token=' + encodeURIComponent(sessionStorage.getItem('token')))
}
//虽然引入的图标是AssignmentReturnedSharpIcon,但是要全部小写,且中间部分大写的地方前面要加下划线
<Button icon="assignment_returned_sharpIcon" variant="contained" color="primary" title="导出" buttonType="button" className="mr-2" onClick={exportData} />}
后端
只记录核心点
//查询结果
List<Map<String, Object>> list = MysqlDaoImpl.getInstance().queryBySql(sql, null, params, null);
// 导出 xlsx
Workbook workbook = new XSSFWorkbook();
// 在工作薄中创建一个工作表
Sheet sheet = workbook.createSheet();
// 设置列宽
sheet.setColumnWidth(0,10*256);
sheet.setColumnWidth(1,20*256);
sheet.setColumnWidth(2,40*256);
sheet.setColumnWidth(3,10*256);
sheet.setColumnWidth(4,15*256);
sheet.setColumnWidth(4,10*256);
sheet.setColumnWidth(4,10*256);
// 处理标题
String[] titles = new String[]{"评估方","调用接口","发起人","评估时间","价格","是否成功","是否平台","是否单页面"};
// 创建标题行
Row titleRow = sheet.createRow(0);
Cell cell = null;
for (int i = 0; i < titles.length; i++) {
cell = titleRow.createCell(i);
cell.setCellValue(titles[i]);
}
// 处理内容
int rowIndex = 1;
Row row = null;
for (Map map : list) {
row = sheet.createRow(rowIndex);
cell = row.createCell(0);
cell.setCellValue(map.get("type")==null?"":map.get("type").toString());
cell = row.createCell(1);
cell.setCellValue(map.get("apiName")==null?"":map.get("apiName").toString());
cell = row.createCell(2);
cell.setCellValue(map.get("name")==null?"":map.get("name").toString());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell = row.createCell(3);
String createTime = map.get("createTime") == null ? "" : map.get("createTime").toString();
if (createTime==""){
cell.setCellValue("");
}else {
Date date = new Date(Long.parseLong(createTime));//必须转化为long类型
cell.setCellValue(dateFormat.format(date));
}
cell = row.createCell(4);
cell.setCellValue(map.get("price")==null?"":map.get("price").toString());
cell = row.createCell(5);
cell.setCellValue(map.get("msg")==null?"":map.get("msg").toString());
cell = row.createCell(6);
cell.setCellValue(isPlatform==null?"全部":isPlatform.equals("ture")?"是":"否");
cell = row.createCell(7);
cell.setCellValue(isSinglePage==null?"全部":isSinglePage.equals("ture")?"是":"否");
rowIndex++;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
// 导出的文件名称
String filename="评估调用明细"+dateFormat.format(new Date())+".xlsx";
// 设置文件的打开方式和mime类型
HttpServletResponse response = ControllerContext.getContext().getResponse();
ServletOutputStream outputStream = response.getOutputStream();
response.setHeader( "Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO8859-1"));
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
workbook.write(outputStream);
}