diff --git a/src/com/lxyer/excel/poi/ExcelKit.java b/src/com/lxyer/excel/poi/ExcelKit.java index 60ce162..c647977 100644 --- a/src/com/lxyer/excel/poi/ExcelKit.java +++ b/src/com/lxyer/excel/poi/ExcelKit.java @@ -43,7 +43,7 @@ public class ExcelKit { * @author LiangXianYou * @date 2015-6-16 下午5:56:56 */ - //map:data,sheetName,hds,hdNames,xlsName; + //map:data,sheetName,hds,hdNames, public static Workbook exportExcels(List> list) throws Exception { Workbook wb = new XSSFWorkbook(); //创建工作薄 @@ -64,12 +64,7 @@ public class ExcelKit { @SuppressWarnings("unchecked") List data = (List) map.get("data"); String[] hds = (String[]) map.get("hds"); - for (int j = 0; j < data.size(); j++) { - for (int k = 0; k < hds.length; k++) { - Object o = getFieldValue(data.get(j), hds[k]); //得到列的值 - data2Excel(sheet, o, j + 1, k); //将值写入Excel - } - } + dataToSheet(sheet, data, hds, 1); } return wb; @@ -100,38 +95,28 @@ public class ExcelKit { } /** - * 参数说明: - * list:list数据集合 - * hdNames:表头需要显示的名称 - * hds:表头对应的对象属性名称,和 hdNames一一对应 - * xlsName:导出Excel的预定义名称 - * request: HttpServletRequest - * response:HttpServletResponse - * 通过数据构建Excel - * + * 使用数据构建 excel 工作薄对象 + * @param list 数据 + * @param hdNames 表头 + * @param hds 每条记录中数据的属性名 + * @param 数据泛型,支持javaBean 或Map + * @return * @throws Exception * @author LiangXianYou * @date 2015-3-13 上午11:00:30 */ public static Workbook exportExcel(List list, String[] hdNames, String[] hds) throws Exception { - Workbook wb = new XSSFWorkbook(); //创建工作薄 - Sheet sheet = wb.createSheet(); //创建工作表 - sheet.autoSizeColumn((short) 0); //自适应宽度 + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet(); //写入表头---Excel的第一行数据 - Row nRow = sheet.createRow(0); //创建行 + Row nRow = sheet.createRow(0); for (int i = 0; i < hdNames.length; i++) { - Cell nCell = nRow.createCell(i); //创建单元格 + Cell nCell = nRow.createCell(i); nCell.setCellValue(hdNames[i]); } - //写入每一行数据---一条记录就是一行数据 - for (int i = 0; i < list.size(); i++) { - for (int j = 0; j < hds.length; j++) { - Object o = getFieldValue(list.get(i), hds[j]); //得到列的值 - data2Excel(sheet, o, i + 1, j); //将值写入Excel - } - } + dataToSheet(sheet, list, hds, 1); return wb; } @@ -148,11 +133,11 @@ public class ExcelKit { if (t == null) { v = null; } else if (t instanceof Map) { - v = ((Map) t).get(fieldName); //得到列的值 + v = ((Map) t).get(fieldName); } else { Field field = t.getClass().getDeclaredField(fieldName); - field.setAccessible(true); //暴力反射 - v = field.get(t); //得到字段数据的值 + field.setAccessible(true); + v = field.get(t); } return v; @@ -164,24 +149,38 @@ public class ExcelKit { * @author LiangXianYou * @date 2015-3-13 上午10:37:55 */ - private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c) { - //通过获得sheet中的某一列,有得到,没有创建 - Row nRow = sheet.getRow(r); - if (nRow == null) { - nRow = sheet.createRow(r); - } - Cell nCell = nRow.createCell(c); + private static void dataToSheet(Sheet sheet, List list, String[] hds, int skipRow) { + if (list == null || list.size() == 0) return; + for (int j = 0; j < list.size(); j++) { + for (int k = 0; k < hds.length; k++) { + Row nRow = sheet.getRow(j + skipRow); + if (nRow == null) { + nRow = sheet.createRow(j + 1); + } + Cell cell = nRow.createCell(k); + + try { + Object v = getFieldValue(list.get(j), hds[k]); //得到列的值 + dataToCell(cell, v); //将值写入Excel + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + private static void dataToCell(Cell cell, Object v) { //根据不同类型进行转化,如有其它类型没有考虑周全的,使用发现的时候添加 - if (o instanceof Integer) nCell.setCellValue((Integer) o); - else if (o instanceof Double) nCell.setCellValue((Double) o); - else if (o instanceof Float) nCell.setCellValue((Float) o); - else if (o instanceof String) nCell.setCellValue((String) o); - else if (o instanceof Date) nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o)); - else if (o instanceof Calendar) nCell.setCellValue((Calendar) o); - else if (o instanceof Boolean) nCell.setCellValue((Boolean) o); - else if (o == null) nCell.setCellValue(""); - else nCell.setCellValue(o + ""); + if (v instanceof Integer) cell.setCellValue((Integer) v); + else if (v instanceof Double) cell.setCellValue((Double) v); + else if (v instanceof Float) cell.setCellValue((Float) v); + else if (v instanceof String) cell.setCellValue((String) v); + else if (v instanceof Date) cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(v)); + else if (v instanceof Calendar) cell.setCellValue((Calendar) v); + else if (v instanceof Boolean) cell.setCellValue((Boolean) v); + else if (v == null) cell.setCellValue(""); + else cell.setCellValue(v + ""); } //======================= 读取excel ===============================