package com.repair.common.utils; import org.apache.poi.hpsf.Decimal; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.time.LocalDate; import java.util.*; /** * @Author: binguo * @Date: 2023/7/19 星期三 9:38 * @Description: com.chuanghai.attendance.utils.excel * @Version: 1.0 */ public class ExcelExportUtil { /** * 将 List> 类型的数据导出为 Excel */ public static void createExcel(List> mapList, String filename, String title, HttpServletResponse response) { //获取数据源的 key, 用于获取列数及设置标题 Map map = mapList.get(0); Set stringSet = map.keySet(); ArrayList headList = new ArrayList<>(stringSet); //定义一个新的工作簿 XSSFWorkbook wb = new XSSFWorkbook(); //创建一个Sheet页 XSSFSheet sheet = wb.createSheet(title); //设置行高 sheet.setDefaultRowHeight((short) (2 * 256)); //为有数据的每列设置列宽 for (int i = 0; i < headList.size(); i++) { sheet.setColumnWidth(i, 8000); } //设置单元格字体样式 XSSFFont font = wb.createFont(); font.setFontName("等线"); font.setFontHeightInPoints((short) 16); //在sheet里创建第一行,并设置单元格内容为 title (标题) // XSSFRow titleRow = sheet.createRow(0); // XSSFCell titleCell = titleRow.createCell(0); // titleCell.setCellValue(title); //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 // sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headList.size() - 1)); // // 创建单元格文字居中样式并设置标题单元格居中 // XSSFCellStyle cellStyle = wb.createCellStyle(); // cellStyle.setAlignment(HorizontalAlignment.CENTER); // titleCell.setCellStyle(cellStyle); //获得表格第二行 XSSFRow row = sheet.createRow(0); //根据数据源信息给第二行每一列设置标题 for (int i = 0; i < headList.size(); i++) { XSSFCell cell = row.createCell(i); cell.setCellValue(headList.get(i)); } XSSFRow rows; XSSFCell cells; //循环拿到的数据给所有行每一列设置对应的值 for (int i = 0; i < mapList.size(); i++) { //在这个sheet页里创建一行 rows = sheet.createRow(i + 1); //给该行数据赋值 for (int j = 0; j < headList.size(); j++) { String value; if (mapList.get(i).get(headList.get(j)) !=null){ value = mapList.get(i).get(headList.get(j)).toString(); }else { value = ""; } cells = rows.createCell(j); cells.setCellValue(value); } } try { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");//application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=".concat(filename)); OutputStream out = response.getOutputStream(); wb.write(out); wb.close(); out.close();; } catch (IOException e) { e.printStackTrace(); } } /** * @description 考虑到下载方式的不同 这里细化成只获取一个workBook 格式为默认 无格式 * @param list 需要存储为excel的对象集合 * @param map 键值对映射 属性 - 表头字段 类似于: name - 姓名 * @param type 生成workbook的类型 * @param tableName 生成Sheet的名称 * @param orderList 表头顺序对应的属性list * @return org.apache.poi.ss.usermodel.Workbook **/ public static Workbook createWorkbook( List list , //数据库查询的返回List Map map , //表头映射 Integer type , //生成workbook的类型 0 - xls 其他-xlsx String tableName , //表名 List orderList //排序的List 为空 则使用默认的顺序 ) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { //工作簿 Workbook workbook = getWorkbookByType(type); //单个表 Sheet sheet = workbook.createSheet(tableName); //orderList 表头顺序,可以为空 if(orderList == null || orderList.size() == 0) { orderList = new ArrayList<>(); //获取map映射的顺序 for (Map.Entry mapEntry : map.entrySet()) { orderList.add(mapEntry.getKey()); } } //每列对应的属性参数 List typeClassList = getParamsType(list.get(0).getClass(), orderList); //设置表头的值 Row headRow = sheet.createRow(0); for (int i = 0; i < orderList.size(); i++) { Cell cell = headRow.createCell(i); //设置单元格的属性 cell.setCellValue(map.get(orderList.get(i))); } int index = 1; //单元行 Row dataRow = null; //单元格 Cell dataCell = null; //遍历List for (T t : list) { //获取一行 dataRow = sheet.createRow(index); //遍历表头对应属性,给一行数据设置值 for (int j = 0; j < orderList.size(); j++) { //获取一个单元格 dataCell = dataRow.createCell(j); //根据对应列的属性 设置对应的值类型及值 setCellValueTypeAndValue(dataCell , typeClassList.get(j) , t.getClass().getMethod(getGetterMethodName(orderList.get(j)) , new Class[]{}) .invoke(t , new Class[]{})); } //行下标后移 index++; } //设置值 return workbook; } /** * @description 根据所给的type获取对应的工作簿 0-HSSFWorkbook 其他-XSSFWorkbook * @author 三文鱼先生 * @date 10:41 2022/8/26 * @param type 类型 * @return org.apache.poi.ss.usermodel.Workbook **/ public static Workbook getWorkbookByType(Integer type) { if(type == null || type == 0) return new HSSFWorkbook(); else return new XSSFWorkbook(); } /** * @description 根据属性的List获取对应的类型List * @author 三文鱼先生 * @date 10:37 2022/8/26 * @param cs 对应的类 * @param paramsList 对应的属性list * @return java.util.List **/ public static List getParamsType(Class cs , List paramsList) { List typeClass = new ArrayList<>(); //对象的所有属性 Field[] fields = cs.getDeclaredFields(); //临时的属性 - 类型映射 Map map = new HashMap(); //获取属性名称及类型 for (Field field : fields) { map.put(field.getName(), field.getType()); } //遍历属性List获取对应的类型List for (String s : paramsList) { typeClass.add(map.get(s)); } return typeClass; } /** * @description 根据对应的类型 给单元格设置类型和值 * @author 三文鱼先生 * @date 10:32 2022/8/26 * @param cell 单元格 * @param cs 属性的类型 * @param o get方法获取到的对象 * @return void **/ public static void setCellValueTypeAndValue(Cell cell , Class cs , Object o) { if(Boolean.class.equals(cs) || boolean.class.equals(cs)) { //boolean类型 cell.setCellValue((Boolean) o); } else if (int.class.equals(cs) || Integer.class.equals(cs)) { //int类型 cell.setCellValue((Integer) o); } else if(double.class.equals(cs) || Double.class.equals(cs)) { //浮点数类型 也可以是float类型什么的 cell.setCellValue((Double) o); }else if(BigDecimal.class.equals(cs) || Decimal.class.equals(cs)){ //金钱类型 cell.setCellValue(((BigDecimal) o).toString()); } else if(LocalDate.class.equals(cs) || Date.class.equals(cs)){ //日期类型 cell.setCellValue(((LocalDate) o).toString()); } else { //默认为字符串类型 cell.setCellValue((String) o); } } /** * @description 根据属性名称获取对应的get方法 * @author 三文鱼先生 * @date 10:38 2022/8/26 * @param param 属性名称 * @return java.lang.String **/ public static String getGetterMethodName(String param) { char[] chars = param.toCharArray(); //首字母大写 if(Character.isLowerCase(chars[0])) { chars[0] -= 32; } //拼接get方法 return "get" + new String(chars); } }