/** * 注解用于Excel * Created by tangCL on 2016/9/8. */ @Retention(RetentionPolicy.RUNTIME)/*运行时有效*/ @Target(ElementType.FIELD)/*范围*/ public @interface ExcelAttribute { /** * Excel中的列名 * * @return */ public abstract String name(); /** * 列名对应的A,B,C,D...,不指定按照默认顺序排序 * * @return */ public abstract String column() default ""; /** * 提示信息 * * @return */ public abstract String prompt() default ""; /** * 设置只能选择不能输入的列内容 * * @return */ public abstract String[] combo() default {}; /** * 是否导出数据 * * @return */ public abstract boolean isExport() default true; }
----------------------------------------------------------------------------------------------------------------
/** * Created by tangCL on 2016/9/8. */ public class ExcelTestVO { @ExcelAttribute(name="ID",isExport=true) private Integer id; @ExcelAttribute(name="姓名",isExport=true) private String name; @ExcelAttribute(name="电话",isExport=true) private String phone; @ExcelAttribute(name="年龄",isExport=true) private Integer age; @ExcelAttribute(name="备注") private String remark; //get,set }----------------------------------------------------------------------------------------------------------------
/** * 采用的是POI3.8以上的SXSSFWorkbook excel版本在2007以上 * Created by tangCL on 2016/9/8. */ public class ExcelUtil<T> implements Serializable { private static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class); public final static int sheetSize = 2; public final static int diskSize = 100000; /** * 获取相应的类 */ private Class<T> clazz; public ExcelUtil(Class<T> clazz) { this.clazz = clazz; } /** * * @param resultList 将写入EXCEL的数据 * @param sheetName 工作表名字 * @param outputStream 输出流 * @return */ public boolean writeExcelFromList(List<T> resultList, String sheetName, OutputStream outputStream){ //返回标示 Boolean sign = Boolean.TRUE; try{ // 得到所有定义字段 Field[] allFields = clazz.getDeclaredFields(); List<Field> fields = new ArrayList<Field>(); // 得到所有field并存放到一个list中 for (Field field : allFields) { if (field.isAnnotationPresent(ExcelAttribute.class)) { fields.add(field); } } // 产生工作薄对象 Workbook workbook = new SXSSFWorkbook(diskSize); //数据源数量 int listSize = 0; if (resultList != null && resultList.size() >= 0) { listSize = resultList.size(); } //工作簿页数 double sheetNo = Math.ceil(listSize / sheetSize); for(int i = 0 ; i <= sheetNo ; i++){ //创建工作簿 Sheet sheet = workbook.createSheet(); //设置工作表的名称 workbook.setSheetName(i,sheetName+""+i); //创建 Row row; Cell cell; //创建第一行 row = sheet.createRow(0); for(int cellNum = 0 ; cellNum < fields.size() ; cellNum++){ // Field field = fields.get(cellNum); //获取注解信息 ExcelAttribute attr = field.getAnnotation(ExcelAttribute.class); int col = cellNum; // 根据指定的顺序获得列号 if (StringUtils.isNotBlank(attr.column())) { col = getExcelCol(attr.column()); } // 创建列 cell = row.createCell(col); sheet.setColumnWidth(i, (int) ((attr.name().getBytes().length <= 4 ? 6 : attr.name().getBytes().length) * 1.5 * 256)); // 设置列中写入内容为String类型 cell.setCellType(Cell.CELL_TYPE_STRING); // 写入列名 cell.setCellValue(attr.name()); /* // 如果设置了提示信息则鼠标放上去提示. if (StringUtils.isNotBlank(attr.prompt())) { setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, col, col); } // 如果设置了combo属性则本列只能选择不能输入 if (attr.combo().length > 0) { setHSSFValidation(sheet, attr.combo(), 1, 100, col, col); } */ } //创建内容列 int startNo = i * sheetSize; int endNo = Math.min(startNo + sheetSize, listSize); for(int j = startNo; j < endNo; j++){ row = sheet.createRow(j + 1 - startNo); // 得到导出对象. T vo = (T) resultList.get(j); for(int k = 0 ; k < fields.size() ; k++){ // 获得field Field field = fields.get(k); // 设置实体类私有属性可访问 field.setAccessible(true); ExcelAttribute attr = field.getAnnotation(ExcelAttribute.class); int col = k; // 根据指定的顺序获得列号 if (StringUtils.isNotBlank(attr.column())) { col = getExcelCol(attr.column()); } cell = row.createCell(col); // 如果数据存在就填入,不存在填入空格 Class<?> classType = (Class<?>) field.getType(); String value = null; if (field.get(vo) != null && classType.isAssignableFrom(Date.class)) { SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); value = DateUtils.formatDate(sdf.parse(String.valueOf(field.get(vo)))); } cell.setCellValue(field.get(vo) == null ? "" : value == null ? String.valueOf(field.get(vo)) : value); } } } outputStream.flush(); workbook.write(outputStream); outputStream.close(); }catch (Exception e){ logger.warn("Excel writeExcelFromList Exception" + e); }finally { return sign; } } /** * 将EXCEL中A,B,C,D,E列映射成0,1,2,3 * * @param col */ public static int getExcelCol(String col) { col = col.toUpperCase(); // 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。 int count = -1; char[] cs = col.toCharArray(); for (int i = 0; i < cs.length; i++) { count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i); } return count; } }
----------------------------------------------------------------------------------------------------------------