前台:
$(‘#btnExport‘).click(function(){
location.href ="${ctx}/hvac/hvacEnergySummary/exportExcel?year=2021&month=5";
});
注意:要用location.href,不要用ajax,否则浏览器没有下载记录
后台:
/**
* excel导出记录
*
* @param request
* @param resp
* @throws UnsupportedEncodingException
*/
public void exportExcel(HttpServletRequest request, HttpServletResponse resp, List<HvacEnergySummary> listContent,String fileName ,String sheetName) throws UnsupportedEncodingException {
HSSFWorkbook wb = new HSSFWorkbook();
request.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/x-download");
fileName = URLEncoder.encode(fileName, "UTF-8");
resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
HSSFSheet sheet = wb.createSheet(sheetName);
sheet.setDefaultRowHeight((short) (2 * 256));
sheet.setColumnWidth(0, 50 * 160);
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
HSSFRow row = sheet.createRow((int) 0);
sheet.createRow((int) 1);
sheet.createRow((int) 2);
sheet.createRow((int) 3);
sheet.createRow((int) 4);
sheet.createRow((int) 5);
sheet.createRow((int) 6);
sheet.createRow((int) 7);
sheet.createRow((int) 8);
sheet.createRow((int) 9);
sheet.createRow((int) 10);
sheet.createRow((int) 11);
sheet.createRow((int) 12);
sheet.createRow((int) 13);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell(0);
cell.setCellValue("设备编号 ");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("设备名称 ");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellStyle(style);
cell.setCellValue("网关");
cell = row.createCell(3);
cell.setCellStyle(style);
cell.setCellValue("系统");
cell = row.createCell(4);
cell.setCellStyle(style);
cell.setCellValue("时段1能耗");
cell = row.createCell(5);
cell.setCellStyle(style);
cell.setCellValue("时段1电费 ");
cell = row.createCell(6);
cell.setCellStyle(style);
cell.setCellValue("时段2能耗 ");
cell = row.createCell(7);
cell.setCellStyle(style);
cell.setCellValue("时段2电费 ");
cell = row.createCell(8);
cell.setCellStyle(style);
cell.setCellValue("时段3能耗");
cell = row.createCell(9);
cell.setCellStyle(style);
cell.setCellValue("时段3电费");
cell = row.createCell(10);
cell.setCellStyle(style);
cell.setCellValue("时段4能耗 ");
cell = row.createCell(11);
cell.setCellStyle(style);
cell.setCellValue("时段4电费");
cell = row.createCell(12);
cell.setCellStyle(style);
cell.setCellValue("总能耗");
cell = row.createCell(13);
cell.setCellStyle(style);
cell.setCellValue("总电费");
List<HvacEnergySummary> vUserOrder = listContent;
for (int i = 0; i < vUserOrder.size(); i++) {
HSSFRow row1 = sheet.createRow((int) i + 1);
HvacEnergySummary vuserOrder = vUserOrder.get(i);
row1.createCell(0).setCellValue(vuserOrder.getEquipCode());
row1.createCell(1).setCellValue(vuserOrder.getEquipName());
row1.createCell(2).setCellValue(vuserOrder.getGatewayCode());
row1.createCell(3).setCellValue(vuserOrder.getSystemCode());
row1.createCell(4).setCellValue(vuserOrder.getTime1EnergyValue());
row1.createCell(5).setCellValue(vuserOrder.getTime1EnergyCost());
row1.createCell(6).setCellValue(vuserOrder.getTime2EnergyValue());
row1.createCell(7).setCellValue(vuserOrder.getTime2EnergyCost());
row1.createCell(8).setCellValue(vuserOrder.getTime3EnergyValue());
row1.createCell(9).setCellValue(vuserOrder.getTime3EnergyCost());
row1.createCell(10).setCellValue(vuserOrder.getTime4EnergyValue());
row1.createCell(11).setCellValue(vuserOrder.getTime4EnergyCost());
row1.createCell(12).setCellValue(vuserOrder.getTotalEnergyValue());
row1.createCell(13).setCellValue(vuserOrder.getTotalEnergyCost());
}
try {
OutputStream out = resp.getOutputStream();
wb.write(out);
out.close();
} catch (ServiceException e) {
logger.info("=====导出excel异常====");
} catch (Exception e1) {
logger.info("=====导出excel异常====");
}
}
原文:https://www.cnblogs.com/lingmin/p/14929723.html