创建模板-------放到maven工程-----sec/main/resources/model目录下
第一步:在maven中导入poi 依赖
org.apache.poi
poi
3.9
第二步:controller层
/**
* 体检对账对账–下载表格mecExportExcel(导出excel)
*
* @param flag
* @author lipeng
* @date 2008.10.29
*/
@RequestMapping("/mecExportExcel")
public void mecExportExcel(String flag, HttpServletResponse responses) {
List<Map<String, Object>> list = null;
Map<String, Object> map = new HashMap<>();
if (StringUtils.isNotBlank(flag)) {
map.put(“flag”, flag);
list = accountService.mecExportExcel(map);
} else {// 查询所有
map.put(“flag”, “NULL”);
list = accountService.mecExportExcel(map);//查询数据源list
}
exportExcelMEC(“tjdzz.xls”, list, responses);
}
//导出excel具体方法
public void exportExcelMEC(String path, List<Map<String, Object>> list, HttpServletResponse responses) {
OutputStream out = null;
String name = “体检对账”;
String format3 = name + “_” + CalendarUtil.format3() + “.xlsx”;// CalendarUtil.format3():获取毫秒值用于导出的excel做名////称拼接使用,
try {
//获取模板在容器中的绝对路径
String filePath = AccountController.class.getClassLoader().getResource(“model” + “/” + path).getPath();
// 获取模板
Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filePath)));
Sheet sheet = workbook.getSheetAt(0);
Row rowCellStyle = sheet.getRow(2);
HSSFCellStyle columnOne = (HSSFCellStyle) rowCellStyle.getCell(0).getCellStyle();//获取模板第1列单元格样式(下标从0开始)
HSSFCellStyle columnOne1 = (HSSFCellStyle) rowCellStyle.getCell(1).getCellStyle();//获取模板第2列单元格样式(下标从0开始)
HSSFCellStyle columnOne2 = (HSSFCellStyle) rowCellStyle.getCell(2).getCellStyle();//获取模板第3列单元格样式(下标从0开始)
HSSFCellStyle columnOne3 = (HSSFCellStyle) rowCellStyle.getCell(3).getCellStyle();//获取模板第4列单元格样式(下标从0开始)
HSSFCellStyle columnOne4 = (HSSFCellStyle) rowCellStyle.getCell(4).getCellStyle();//获取模板第5列单元格样式(下标从0开始)
HSSFCellStyle columnOne5 = (HSSFCellStyle) rowCellStyle.getCell(5).getCellStyle();//获取模板第6列单元格样式(下标从0开始)
HSSFCellStyle columnOne6 = (HSSFCellStyle) rowCellStyle.getCell(6).getCellStyle();//获取模板第7列单元格样式(下标从0开始)
HSSFCellStyle columnOne7 = (HSSFCellStyle) rowCellStyle.getCell(7).getCellStyle();//获取模板第8列单元格样式(下标从0开始)
HSSFCellStyle columnOne8 = (HSSFCellStyle) rowCellStyle.getCell(8).getCellStyle();//获取模板第9列单元格样式(下标从0开始)
HSSFCellStyle columnOne9 = (HSSFCellStyle) rowCellStyle.getCell(9).getCellStyle();//获取模板第10列单元格样式(下标从0开始)
int num = 0;
for (int i = 0; i < list.size(); i++) {
HSSFRow dataRow = (HSSFRow) sheet.createRow(sheet.getLastRowNum() + 1);
Map<String, Object> map = list.get(i);
HSSFCell cell0 = dataRow.createCell(0);
cell0.setCellValue(Integer.toString(num += 1));// 序号
cell0.setCellStyle(columnOne);// 填充样式
HSSFCell cell1 = dataRow.createCell(1);// 流水号
String disp_id = (String) map.get("disp_id");
if (StringUtils.isNotBlank(disp_id)) {
cell1.setCellValue(disp_id);
cell1.setCellStyle(columnOne1);// 填充样式
} else {
cell1.setCellValue("");// 流水号
cell1.setCellStyle(columnOne1);// 填充样式
}
HSSFCell cell2 = dataRow.createCell(2);//// 订单编号
String medical_order_number = (String) map.get("medical_order_number");
if (StringUtils.isNotBlank(medical_order_number)) {
cell2.setCellValue(medical_order_number);
cell2.setCellStyle(columnOne2);// 填充样式
} else {
cell2.setCellValue("");// 流水号
cell2.setCellStyle(columnOne2);// 填充样式
}
HSSFCell cell3 = dataRow.createCell(3);// 下单时间
Date create_date = (Date) map.get("create_date");
if (StringUtils.isNotBlank(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(create_date))) {
cell3.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(create_date));
cell3.setCellStyle(columnOne3);// 填充样式
} else {
cell3.setCellValue("");
cell3.setCellStyle(columnOne3);// 填充样式
}
HSSFCell cell4 = dataRow.createCell(4);// 订单总金额
Double medical_money = (Double) map.get("medical_money");
if (StringUtils.isNotBlank(medical_money.toString())) {
cell4.setCellValue(medical_money.toString());
cell4.setCellStyle(columnOne4);// 填充样式
} else {
cell4.setCellValue("");
cell4.setCellStyle(columnOne4);// 填充样式
}
HSSFCell cell5 = dataRow.createCell(5);// 体检名称
String medical_set_meal = (String) map.get("medical_set_meal");
if (StringUtils.isNotBlank(medical_set_meal)) {
cell5.setCellValue(medical_set_meal);
cell5.setCellStyle(columnOne5);// 填充样式
} else {
cell5.setCellValue("");
cell5.setCellStyle(columnOne5);// 填充样式
}
HSSFCell cell6 = dataRow.createCell(6);// 补贴金额
Double medical_subsidy = (Double) map.get("medical_subsidy");
if (null != medical_subsidy) {
cell6.setCellValue(medical_subsidy.toString());
cell6.setCellStyle(columnOne6);// 填充样式
} else {
cell6.setCellValue("");
cell6.setCellStyle(columnOne6);// 填充样式
}
HSSFCell cell7 = dataRow.createCell(7);// 患者姓名
String realname = (String) map.get("realname");
if (StringUtils.isNotBlank(realname)) {
cell7.setCellValue(realname);
cell7.setCellStyle(columnOne7);// 填充样式
} else {
cell7.setCellValue("");
cell7.setCellStyle(columnOne7);// 填充样式
}
HSSFCell cell8 = dataRow.createCell(8);
if (map.get("disp_status").toString().equals("1")) {// 对账状态
cell8.setCellValue("已对账");
cell8.setCellStyle(columnOne8);// 填充样式
} else {
cell8.setCellValue("未对账");
cell8.setCellStyle(columnOne8);// 填充样式
}
HSSFCell cell9 = dataRow.createCell(9);//// 备注
String disp_description = (String) map.get("disp_description");
if (StringUtils.isNotBlank(disp_description)) {
cell9.setCellValue(disp_description);
cell9.setCellStyle(columnOne9);// 填充样式
} else {
cell9.setCellValue("");
cell9.setCellStyle(columnOne9);// 填充样式
}
}
// 弹出下载方式
responses.reset();
responses.setContentType("application/msexcel");
responses.setCharacterEncoding("UTF-8");
responses.setHeader("Content-Disposition",
"attchment;filename=" + new String((format3.getBytes()), "iso-8859-1"));
out = responses.getOutputStream();
workbook.write(out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}