@ApiOperation("反查下载")
@PostMapping("/pegging/download")
public void downloadPegging(HttpServletRequest request,
HttpServletResponse response,
@RequestBody PeggingQueryVo peggingQueryVo) throws IOException {
//获取表头携带的信息
SchemaIndexVo infoDataOne = peggingQueryVo.getInfoDataOne();
PeggingHeadVo infoDataTwo = peggingQueryVo.getInfoDataTwo();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String fileName;
String sheetName;
if (StrUtil.isNotBlank(peggingQueryVo.getRelIndexId())) {
//第二层反查下载文件命名规则 “评估方案名称_”+“统计周期_”+“维度_”+“维度名称_”+“质效指标名称_”+“基础指标名称_”+“系统当前日期”
fileName = infoDataOne.getSchemaName() + "_" + peggingQueryVo.getStartTime() + "至" + peggingQueryVo.getEndTime() + "_" + peggingQueryVo.getType() + "_" + infoDataOne.getIndexName() + "_" + infoDataTwo.getName() + "_" + format.format(new Date());
//获取指标名称
sheetName = peggingQueryVo.getInfoDataTwo().getName() + "反查信息";
} else {
//第一层反查下载文件命名规则 “评估方案名称_”+“统计周期_”+“维度_”+“质效指标名称_”+“系统当前日期”
fileName = infoDataOne.getSchemaName() + "_" + peggingQueryVo.getStartTime() + "至" + peggingQueryVo.getEndTime() + "_" + peggingQueryVo.getType() + "_" + infoDataOne.getIndexName() + "_" + format.format(new Date());
//获取指标名称
sheetName = peggingQueryVo.getInfoDataOne().getIndexName() + "反查信息";
}
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setCharacterEncoding("utf-8");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=" + ExcelUtils.encodeFileName(request.getHeader("User-Agent"),
fileName));
statisticsService.peggingDownload(peggingQueryVo, response, sheetName);
}
/**
* 反查下载
*
* @param peggingQueryVo 反查查询条件
* @param response 响应流
* @param sheetName
*/
public void peggingDownload(PeggingQueryVo peggingQueryVo, HttpServletResponse response, String sheetName) throws IOException {
//获取表头数据
List<PeggingHeadVo> peggingHeadVoList = peggingService.getPeggingHead(peggingQueryVo);
//获取列表数据
Page<Map<String, String>> dataPage = peggingService.getPeggingData(peggingQueryVo, peggingHeadVoList);
List<Map<String, String>> dataList = dataPage.getResult();
//下载
ExcelUtils.downExcel(dataList, peggingHeadVoList, sheetName, response);
}
/**
* 无合并单元格的下载
*
* @param dataList 数据源
* * @param columns 字段名
* * @param indexList 指标名
* * @param sheetName 页签名字
* * @param tableName 表头名字
* * @param response 响应流
* throws IOException
*/
public static void downExcel(List<Map<String, String>> dataList, List<PeggingHeadVo> columns, String sheetName, HttpServletResponse response) throws IOException {
//数据处理,动态生成excel
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
//
workbook.setSheetName(0, sheetName);
//第一行作为表头行的时候的设置
HSSFCellStyle styleTitle = ExcelUtils.getTitleStyle(workbook);//标题样式
HSSFCellStyle styleContent = ExcelUtils.getContentStyle(workbook, "");//正文样式
//表头数据
//第一行
Row titleRowOne = sheet.createRow(0);
sheet.setDefaultColumnWidth(15);
titleRowOne.setHeightInPoints(30f);
for (int i = 0; i < columns.size(); i++) {
Cell typeCell = titleRowOne.createCell(i);
typeCell.setCellValue(sheetName);
typeCell.setCellStyle(styleTitle);
}
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, columns.size() - 1);
sheet.addMergedRegion(cellRangeAddress);
ExcelUtils.setBorderForMergeCell(BorderStyle.THIN, cellRangeAddress, sheet);
//第二行
Row titleRow = sheet.createRow(1);
sheet.setDefaultColumnWidth(15);
titleRow.setHeightInPoints(30f);
for (int i = 0; i < columns.size(); i++) {
String column = columns.get(i).getName();
Cell typeCell = titleRow.createCell(i);
typeCell.setCellValue(column);
typeCell.setCellStyle(styleTitle);
}
int row = 2;
//获取表格输入的字段--必须按表格每列实际顺序存入数值
for (Map<String, String> map : dataList) {
Row rowItem = sheet.createRow(row);
for (int s = 0; s < columns.size(); s++) {
Cell cell = rowItem.createCell(s);
cell.setCellValue("" + (map.get(columns.get(s).getField()) == null ? "" : map.get(columns.get(s).getField())));
cell.setCellStyle(styleContent);
}
row++;
}
workbook.write(response.getOutputStream());
}