工具类
导出工具类
package com.epoch.boot.customproject.demand.util; import org.apache.poi.hssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; public class CustomExcelUtils { public static void exportExcel(HttpServletResponse response, String fileName, List<Map> dataList, String[] titleName, String[] titleCode) throws Exception { if (CustomUtils.isEmpty(dataList)) { response.addHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().print("没有可导出数据!"); return; } int fileSize = 65535; double sheetNum = Math.ceil(dataList.size() / new Integer(fileSize).doubleValue()); HSSFWorkbook wb = new HSSFWorkbook(); try { for (int i = 0; i < sheetNum; i++) { HSSFSheet sheet = wb.createSheet(fileName + (i + 1)); if (sheetNum == 1) { fillSheet(sheet, dataList, titleName, titleCode); } else { int subSize = i + 1 == sheetNum ? dataList.size() : fileSize; List<Map> subList = dataList.subList(i * fileSize, subSize); fillSheet(sheet, subList, titleName, titleCode); } } } catch (Exception e) { e.printStackTrace(); } response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "iso-8859-1") + ".xls"); response.flushBuffer(); wb.write(response.getOutputStream()); } public static void fillSheet(HSSFSheet sheet, List<Map> dataList, String[] titleName, String[] titleCode) { //标题行 HSSFRow titleRow = sheet.createRow(0); //添加标题行数据 for (int i = 0; i < titleName.length; i++) { titleRow.createCell(i).setCellValue(new HSSFRichTextString(titleName[i])); } //插入数据 for (int i = 0; i < dataList.size(); i++) { Map data = dataList.get(i); HSSFRow row = sheet.createRow(i + 1); for (int j = 0; j < titleCode.length; j++) { if (CustomUtils.isNotEmpty(data.get(titleCode[j]))) { row.createCell(j).setCellValue(data.get(titleCode[j]).toString()); } } } } }
判空工具类
package com.epoch.boot.customproject.demand.util; import org.springframework.lang.Nullable; import java.util.Collection; import java.util.Map; public class CustomUtils { public static boolean isEmpty(Object obj) { return (obj == null || "".equals(obj) || "null".equals(obj)); } public static boolean isNotEmpty(Object obj) { return !isEmpty(obj); } public static boolean isEmpty(@Nullable Map<?, ?> map) { return (map == null || map.isEmpty()); } public static boolean isNotEmpty(@Nullable Map<?, ?> map) { return !isEmpty(map); } public static boolean isEmpty(@Nullable Collection<?> collection) { return (collection == null || collection.isEmpty()); } public static boolean isNotEmpty(@Nullable Collection<?> collection) { return !isEmpty(collection); } }

浙公网安备 33010602011771号