说明:从数据库查询数据集写入数据到excel文件,并设置excel导出样式。记录一下,不喜勿喷。
@RequestMapping("/fileDown")
public @ResponseBody
WrappedResult fileDown(@RequestParam(value = "appNoQfbd") String appNoQfbd,@RequestParam(value = "eventCode") String eventCode,
@RequestParam(value = "ym") String ym,HttpServletResponse response,HttpServletRequest request) throws Exception {
GasfeeComCondition condition = new GasfeeComCondition();
condition.setAppNoQfbd(appNoQfbd);
condition.setEventCode(eventCode);
condition.setYm(ym);
gasfeeComService.fileDown(response, condition);
return new WrappedResult(true, true, "");
}
@SuppressWarnings("deprecation")
public void fileDown(HttpServletResponse response, GasfeeComCondition condition) throws IOException {
// create HSSFWorkbook EXCEL设置部分
HSSFWorkbook workbook = new HSSFWorkbook();
//默认黑色字体
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体
font.setFontHeightInPoints((short) 11);//字号
//红色字体
HSSFFont redFont = workbook.createFont();
redFont.setFontName("宋体");
//redFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体
redFont.setColor(HSSFFont.COLOR_RED);
redFont.setFontHeightInPoints((short) 10);//字号
//单元格样式(非必填)
HSSFCellStyle titleStyleNo = workbook.createCellStyle();
//灰色填充
titleStyleNo.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
titleStyleNo.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyleNo.setFont(font);
titleStyleNo.setBorderBottom((short)1);//下边框
titleStyleNo.setBorderLeft((short)1);//左边框
titleStyleNo.setBorderRight((short)1);//右边框
titleStyleNo.setBorderTop((short)1);//上边框
titleStyleNo.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
titleStyleNo.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//单元格样式(必填)
HSSFCellStyle titleStyleYes = workbook.createCellStyle();
//灰色填充
titleStyleYes.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
titleStyleYes.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titleStyleYes.setFont(redFont);
titleStyleYes.setBorderBottom((short)1);//下边框
titleStyleYes.setBorderLeft((short)1);//左边框
titleStyleYes.setBorderRight((short)1);//右边框
titleStyleYes.setBorderTop((short)1);//上边框
titleStyleYes.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
titleStyleYes.setAlignment(HSSFCellStyle.ALIGN_CENTER);
List<GasQfbdLogVo> listGasLog = null;
// 创建sheet页
HSSFSheet sheet = workbook.createSheet();
String fileName = "";
String[] headList = new String[]{};
if("01".equals(condition.getEventCode())){
headList = new String[]{"工单号","年月","用户号","错误代码","错误内容"};
listGasLog = dao.fileDataObtain(condition);
fileName = "数据获取异常明细.xls";
workbook.setSheetName(0, "数据获取异常明细");
}
//设置表格宽度
for(int i = 0;i < headList.length; i++){
sheet.setColumnWidth((short) i, (short) 5766);
}
//设置第一行标题列
HSSFRow rowTitleList = sheet.createRow(0);
for(int i = 0 ;i < headList.length; i++){
HSSFCell cellTitleList_0 = rowTitleList.createCell((short) i);
cellTitleList_0.setCellType(HSSFCell.CELL_TYPE_STRING);
cellTitleList_0.setCellStyle(titleStyleNo);
cellTitleList_0.setCellValue(headList[i]);
}
//写入数据到excel
if(listGasLog != null){
for(int j = 0 ;j < listGasLog.size();j++){
HSSFRow row = sheet.createRow(j + 1);
for(int n = 0 ;n < headList.length;n++){
HSSFCell cellData = row.createCell((short) n);
cellData.setCellType(HSSFCell.CELL_TYPE_STRING);
cellData.setCellStyle(titleStyleYes);
if(n == 0){
cellData.setCellValue(listGasLog.get(j).getAppNoQfbd());
}
if(n == 1){
cellData.setCellValue(listGasLog.get(j).getYm());
}
if(n == 2){
cellData.setCellValue(listGasLog.get(j).getConsNo());
}
if(n == 3){
cellData.setCellValue(listGasLog.get(j).getOutCode());
}
if(n == 4){
cellData.setCellValue(listGasLog.get(j).getOutMsg());
}
}
}
}
//弹出下载对话框
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
OutputStream out = response.getOutputStream();
workbook.write(out);
//清空缓冲区
out.flush();
out.close();
}