注意事项!此方法必须使用表单提交参数!
public void ExportCwInfo(HttpServletRequest request, HttpServletResponse response) {
String themeId = request.getParameter("themeId");
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
Map<String, Object> paraMap = new HashMap<String, Object>();
paraMap.put("themeId", themeId);
paraMap.put("startTime", startTime);
paraMap.put("endTime", endTime);
List<Map<String, Object>> cwList = service.exportCwInfo(paraMap);
// ------------------------------------样式设置开始------------------------------------
// 创建工作簿
final XSSFWorkbook wb = new XSSFWorkbook();
// 分页名称
final XSSFSheet sheet = wb.createSheet("结果");
// 生成一种样式
final XSSFCellStyle style = wb.createCellStyle();
// 设置样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成一种字体
final XSSFFont font = wb.createFont();
// 设置字体
font.setFontName("微软雅黑");
// 设置字体大小
font.setFontHeightInPoints((short) 12);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 在样式中引用这种字体
style.setFont(font);
// 生成并设置另一个样式
final XSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一种字体2
final XSSFFont font2 = wb.createFont();
// 设置字体
font2.setFontName("微软雅黑");
// 设置字体大小
font2.setFontHeightInPoints((short) 12);
// 在样式2中引用这种字体
style2.setFont(font2);
// 设置列宽
sheet.setColumnWidth(0, 22 * 256);
sheet.setColumnWidth(1, 10 * 256);
sheet.setColumnWidth(2, 5 * 256);
sheet.setColumnWidth(3, 28 * 256);
sheet.setColumnWidth(4, 12 * 256);
sheet.setColumnWidth(5, 25 * 256);
// ------------------------------------样式设置结束------------------------------------
// ------------------------------------填充表头开始------------------------------------
final String[] excelHeaderArr = {"身份证号", "姓名", "性别", "专业", "金额", "支付时间"};
// 生成表格第一行 第一行表头
final XSSFRow row = sheet.createRow(0);
XSSFCell cell = null;
for (int i = 0; i < excelHeaderArr.length; i ++ ) {
cell = row.createCell(i);
// 设置数值
cell.setCellValue(excelHeaderArr[i]);
// 设置样式
cell.setCellStyle(style);
}
// ------------------------------------填充表头结束------------------------------------
// ------------------------------------设置表体开始------------------------------------
for (int i = 0; i < cwList.size(); i ++ ) {
final XSSFRow row1 = sheet.createRow(i + 1);
cell = row1.createCell(0);
cell.setCellValue(cwList.get(i).get("colum_1").toString());
cell.setCellStyle(style2);
cell = row1.createCell(1);
cell.setCellValue(cwList.get(i).get("colum_2").toString());
cell.setCellStyle(style2);
cell = row1.createCell(2);
cell.setCellValue(cwList.get(i).get("colum_3").toString());
cell.setCellStyle(style2);
cell = row1.createCell(3);
cell.setCellValue(cwList.get(i).get("colum_4").toString());
cell.setCellStyle(style2);
cell = row1.createCell(4);
cell.setCellValue(cwList.get(i).get("amount").toString());
cell.setCellStyle(style2);
cell = row1.createCell(5);
cell.setCellValue(cwList.get(i).get("gengxin_date").toString());
cell.setCellStyle(style2);
}
// ------------------------------------设置表体结束------------------------------------
ExcelExportUtil.downloadExcel(response, wb, "cwInfo.xlsx");
下载Excel方法 :
public static void downloadExcel(final HttpServletResponse response, final Workbook workBook, final String filename) {
try {
response.addHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.setContentType("application/octet-stream");
final OutputStream stream = new BufferedOutputStream(response.getOutputStream());
workBook.write(stream);
stream.close();
} catch (final Exception e) {
e.printStackTrace();
}
}