-
通過maven引入jar
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <poi.version>4.1.0</poi.version> <commons-lang3.version>3.5</commons-lang3.version> <guava.version>18.0</guava.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> </dependencies>
-
自定義注解@ExcelField
package cn.lisongyu.common.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author lisongyu * @date 2019/09/23 */ @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelField { /** * 導出字段名(默認調用當前字段的“get”方法,如指定導出字段為對象,請填寫“對象名.對象屬性”,例:“area.name”、“office.name”) */ String value() default ""; /** * 導出字段標題(需要添加批注請用“**”分隔,標題**批注,僅對導出模板有效) */ String title(); /** * 導出字段對齊方式(0:自動;1:靠左;2:居中;3:靠右) */ int align() default 0; /** * 導出字段字段排序(升序) */ int sort() default 0; }
-
工具類
- 導出
package cn.lisongyu.common.util; import cn.lisongyu.common.annotation.ExcelField; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; import java.net.URLEncoder; import java.util.*; /** * @author lisongyu * @date 2019/09/23 */ @Slf4j public class ExportExcel { /** * 工作薄對象 */ private SXSSFWorkbook wb; /** * 工作表對象 */ private Sheet sheet; /** * 樣式列表 */ private Map<String, CellStyle> styles; /** * 當前行號 */ private int rownum; /** * 注解列表(Object[]{ ExcelField, Field/Method }) */ private List<Object[]> annotationList = new ArrayList<>(); /** * 構造函數 * * @param title 表格標題,傳“空值”,表示無標題 * @param cls 實體對象,通過annotation.ExportField獲取標題 */ public ExportExcel(String title, Class<?> cls) { //通過反射獲取類的字段 Field[] fs = cls.getDeclaredFields(); for (Field f : fs) { //獲取類中包含注解的字段 ExcelField ef = f.getAnnotation(ExcelField.class); if (ef != null) { //將注解ExcelField和字段保存 annotationList.add(new Object[]{ef, f}); } } //獲取方法 Method[] ms = cls.getDeclaredMethods(); for (Method m : ms) { ExcelField ef = m.getAnnotation(ExcelField.class); if (ef != null) { //將注解ExcelField和方法名保存 annotationList.add(new Object[]{ef, m}); } } //通過注解中的sort排序 annotationList.sort(Comparator.comparing((Object[] o) -> ((ExcelField) o[0]).sort())); //初始化表格 List<String> headerList = new ArrayList<>(); for (Object[] os : annotationList) { //獲取標題 String t = ((ExcelField) os[0]).title(); headerList.add(t); } initialize(title, headerList); } /** * 初始化表頭 * * @param title title * @param headerList headerList */ private void initialize(String title, List<String> headerList) { this.wb = new SXSSFWorkbook(500); this.sheet = wb.createSheet(title); this.styles = createStyles(wb); // Create title if (StringUtils.isNotBlank(title)) { Row titleRow = sheet.createRow(rownum++); titleRow.setHeightInPoints(30); //創建列 Cell titleCell = titleRow.createCell(0); titleCell.setCellStyle(styles.get("title")); titleCell.setCellValue(title); sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), headerList.size() - 1)); } // Create header if (headerList == null) { throw new RuntimeException("headerList not null!"); } //創建行 Row headerRow = sheet.createRow(rownum++); headerRow.setHeightInPoints(16); for (int i = 0; i < headerList.size(); i++) { Cell cell = headerRow.createCell(i); cell.setCellStyle(styles.get("header")); String[] ss = StringUtils.split(headerList.get(i), "**", 2); if (ss.length == 2) { cell.setCellValue(ss[0]); Comment comment = this.sheet.createDrawingPatriarch().createCellComment( new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6)); comment.setString(new XSSFRichTextString(ss[1])); cell.setCellComment(comment); } else { cell.setCellValue(headerList.get(i)); } if (sheet instanceof SXSSFSheet) { ((SXSSFSheet) sheet).trackAllColumnsForAutoSizing(); } else { sheet.autoSizeColumn(i); } } for (int i = 0; i < headerList.size(); i++) { int colWidth = sheet.getColumnWidth(i) * 2; sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth); } log.debug("Initialize success."); } /** * 創建表格樣式 * * @param wb 工作薄對象 * @return 樣式列表 */ private Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<>(8); CellStyle style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); Font titleFont = wb.createFont(); titleFont.setFontName("Arial"); titleFont.setFontHeightInPoints((short) 16); titleFont.setBold(true); style.setFont(titleFont); styles.put("title", style); style = wb.createCellStyle(); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderTop(BorderStyle.THIN); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); Font dataFont = wb.createFont(); dataFont.setFontName("Arial"); dataFont.setFontHeightInPoints((short) 10); style.setFont(dataFont); styles.put("data", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(HorizontalAlignment.LEFT); styles.put("data1", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(HorizontalAlignment.CENTER); styles.put("data2", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(HorizontalAlignment.RIGHT); styles.put("data3", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(HorizontalAlignment.CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); Font headerFont = wb.createFont(); headerFont.setFontName("Arial"); headerFont.setFontHeightInPoints((short) 10); headerFont.setBold(true); headerFont.setColor(IndexedColors.WHITE.getIndex()); style.setFont(headerFont); styles.put("header", style); return styles; } /** * 添加數據(通過annotation.ExportField添加數據) * * @return list 數據列表 */ public <E> ExportExcel setDataList(List<E> list) { for (E e : list) { int colunm = 0; Row row = this.addRow(); StringBuilder sb = new StringBuilder(); for (Object[] os : annotationList) { ExcelField ef = (ExcelField) os[0]; Object val = null; // Get entity value try { if (StringUtils.isNotBlank(ef.value())) { val = Reflections.invokeGetter(e, ef.value()); } else { if (os[1] instanceof Field) { val = Reflections.invokeGetter(e, ((Field) os[1]).getName()); } else if (os[1] instanceof Method) { val = Reflections.invokeMethod(e, ((Method) os[1]).getName(), new Class[]{}, new Object[]{}); } } } catch (Exception ex) { log.info(ex.toString()); val = ""; } this.addCell(row, colunm++, val, ef.align()); sb.append(val).append(","); } } return this; } /** * 添加一行 * * @return 行對象 */ private Row addRow() { return sheet.createRow(rownum++); } /** * 添加一個單元格 * * @param row 添加的行 * @param column 添加列號 * @param val 添加值 * @param align 對齊方式(1:靠左;2:居中;3:靠右) * @return 單元格對象 */ private Cell addCell(Row row, int column, Object val, int align) { Cell cell = row.createCell(column); CellStyle style = styles.get("data" + (align >= 1 && align <= 3 ? align : "")); try { if (val == null) { cell.setCellValue(""); } else if (val instanceof Integer) { cell.setCellValue((Integer) val); } else if (val instanceof Long) { cell.setCellValue((Long) val); } else if (val instanceof Double) { cell.setCellValue((Double) val); } else if (val instanceof BigDecimal) { cell.setCellValue(val.toString()); } else if (val instanceof Float) { cell.setCellValue((Float) val); } else if (val instanceof Date) { DataFormat format = wb.createDataFormat(); style.setDataFormat(format.getFormat("yyyy-MM-dd")); cell.setCellValue((Date) val); } else { cell.setCellValue((String) val); } } catch (Exception ex) { log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString()); assert val != null; cell.setCellValue(val.toString()); } cell.setCellStyle(style); return cell; } /** * 輸出數據流 * * @param os 輸出數據流 */ public ExportExcel write(OutputStream os) throws IOException { wb.write(os); return this; } /** * 輸出到客戶端 * * @param fileName 輸出文件名 */ public ExportExcel write(HttpServletResponse response, String fileName) throws IOException { response.reset(); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8")); write(response.getOutputStream()); return this; } /** * 輸出到客戶端 解決亂碼問題 * * @param fileName 輸出文件名 */ public ExportExcel write2(HttpServletResponse response, String fileName) throws IOException { fileName = new String(fileName.getBytes(), "ISO8859-1"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); write(response.getOutputStream()); return this; } /** * 輸出到文件 * * @param fileName 輸出文件名 */ public void writeFile(String fileName) throws IOException { FileOutputStream os = new FileOutputStream(fileName); this.write(os); } /** * 清理臨時文件 */ public ExportExcel dispose() { wb.dispose(); return this; } }
- 導入
package cn.lisongyu.common.util; import cn.lisongyu.common.annotation.ExcelField; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; /** * 導入Excel文件(支持“XLS”和“XLSX”格式) * * @author */ public class ImportExcel { private static Logger log = LoggerFactory.getLogger(ImportExcel.class); /** * 工作薄對象 */ private Workbook wb; /** * 工作表對象 */ private Sheet sheet; /** * 標題行號 */ private int headerNum; /** * 構造函數 * * @param fileName 導入文件,讀取第一個工作表 * @param headerNum 標題行號,數據行號=標題行號+1 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, int headerNum) throws InvalidFormatException, IOException { this(new File(fileName), headerNum); } /** * 構造函數 * * @param file 導入文件對象,讀取第一個工作表 * @param headerNum 標題行號,數據行號=標題行號+1 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(File file, int headerNum) throws InvalidFormatException, IOException { this(file, headerNum, 0); } /** * 構造函數 * * @param fileName 導入文件 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工作表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(new File(fileName), headerNum, sheetIndex); } /** * 構造函數 * * @param file 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工作表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(File file, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(file.getName(), new FileInputStream(file), headerNum, sheetIndex); } /** * 構造函數 * * @param multipartFile 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工作表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex); } /** * 構造函數 * * @param fileName 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工作表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { if (StringUtils.isBlank(fileName)) { throw new RuntimeException("導入文檔為空!"); } else if (fileName.toLowerCase().endsWith("xls")) { this.wb = new HSSFWorkbook(is); } else if (fileName.toLowerCase().endsWith("xlsx")) { this.wb = new XSSFWorkbook(is); } else { throw new RuntimeException("文檔格式不正確!"); } if (this.wb.getNumberOfSheets() < sheetIndex) { throw new RuntimeException("文檔中沒有工作表!"); } this.sheet = this.wb.getSheetAt(sheetIndex); this.headerNum = headerNum; } /** * 獲取行對象 * * @param rownum * @return */ public Row getRow(int rownum) { return this.sheet.getRow(rownum); } /** * 獲取數據行號 * * @return */ public int getDataRowNum() { return headerNum + 1; } /** * 獲取最后一個數據行號 * * @return */ public int getLastDataRowNum() { return this.sheet.getLastRowNum() + headerNum; } /** * 獲取最后一個列號 * * @return */ public int getLastCellNum() { return this.getRow(headerNum).getLastCellNum(); } /** * 獲取單元格值 * * @param row 獲取的行 * @param column 獲取單元格列號 * @return 單元格值 */ public Object getCellValue(Row row, int column) { Object val = ""; try { Cell cell = row.getCell(column); if (cell != null) { if (cell.getCellType() == CellType.NUMERIC) { // val = cell.getNumericCellValue(); // 當excel 中的數據為數值或日期是需要特殊處理 if (HSSFDateUtil.isCellDateFormatted(cell)) { double d = cell.getNumericCellValue(); Date date = HSSFDateUtil.getJavaDate(d); SimpleDateFormat dformat = new SimpleDateFormat( "yyyy-MM-dd"); val = dformat.format(date); } else { NumberFormat nf = NumberFormat.getInstance(); // true時的格式:1,234,567,890 nf.setGroupingUsed(false); // 數值類型的數據為double,所以需要轉換一下 val = nf.format(cell.getNumericCellValue()); } } else if (cell.getCellType() == CellType.STRING) { val = cell.getStringCellValue(); } else if (cell.getCellType() == CellType.FORMULA) { val = cell.getCellFormula(); } else if (cell.getCellType() == CellType.BOOLEAN) { val = cell.getBooleanCellValue(); } else if (cell.getCellType() == CellType.ERROR) { val = cell.getErrorCellValue(); } } } catch (Exception e) { return val; } return val; } /** * 獲取導入數據列表 * * @param cls 導入對象類型 */ public <E> List<E> getDataList(Class<E> cls) throws InstantiationException, IllegalAccessException { List<Object[]> annotationList = new ArrayList(); // Get annotation field Field[] fs = cls.getDeclaredFields(); for (Field f : fs) { ExcelField ef = f.getAnnotation(ExcelField.class); if (ef != null) { annotationList.add(new Object[]{ef, f}); } } // Get annotation method Method[] ms = cls.getDeclaredMethods(); for (Method m : ms) { ExcelField ef = m.getAnnotation(ExcelField.class); if (ef != null) { annotationList.add(new Object[]{ef, m}); } } // Field sorting annotationList.sort(Comparator.comparing((Object[] o) -> ((ExcelField) o[0]).sort())); List<E> dataList = new ArrayList<>(); for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) { E e = (E) cls.newInstance(); int column = 0; Row row = this.getRow(i); StringBuilder sb = new StringBuilder(); for (Object[] os : annotationList) { Object val = this.getCellValue(row, column++); if (val != null) { Class<?> valType = Class.class; if (os[1] instanceof Field) { valType = ((Field) os[1]).getType(); } else if (os[1] instanceof Method) { Method method = ((Method) os[1]); if ("get".equals(method.getName().substring(0, 3))) { valType = method.getReturnType(); } else if ("set".equals(method.getName().substring(0, 3))) { valType = ((Method) os[1]).getParameterTypes()[0]; } } try { //如果導入的java對象,需要在這里自己進行變換。 if (valType == String.class) { String s = String.valueOf(val.toString()); if (StringUtils.endsWith(s, ".0")) { val = StringUtils.substringBefore(s, ".0"); } else { val = String.valueOf(val.toString()); } } else if (valType == Integer.class) { val = Double.valueOf(val.toString()).intValue(); } else if (valType == Long.class) { val = Double.valueOf(val.toString()).longValue(); } else if (valType == Double.class) { val = Double.valueOf(val.toString()); } else if (valType == Float.class) { val = Float.valueOf(val.toString()); } else if (valType == Date.class) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); val = sdf.parse(val.toString()); } else { val = String.valueOf(val); } } catch (Exception ex) { log.info("Get cell value [" + i + "," + column + "] error: " + ex.toString()); val = null; } // set entity value if (os[1] instanceof Field) { Reflections.invokeSetter(e, ((Field) os[1]).getName(), val); } else if (os[1] instanceof Method) { String mthodName = ((Method) os[1]).getName(); if ("get".equals(mthodName.substring(0, 3))) { mthodName = "set" + StringUtils.substringAfter(mthodName, "get"); } Reflections.invokeMethod(e, mthodName, new Class[]{valType}, new Object[]{val}); } } sb.append(val + ", "); } dataList.add(e); log.info("Read success: [{}] ", sb.toString()); } return dataList; } }
- 反射
package cn.lisongyu.common.util; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; /** * 反射工具類. * 提供調用getter/setter方法, 訪問私有變量, 調用私有方法, 獲取泛型類型Class, 被AOP過的真實類等工具函數. * * @author lisongyu * @date 2019/09/23 */ @SuppressWarnings("rawtypes") public class Reflections { private static final String SETTER_PREFIX = "set"; private static final String GETTER_PREFIX = "get"; private static final String CGLIB_CLASS_SEPARATOR = "$$"; private static final String POINT = "."; private static Logger logger = LoggerFactory.getLogger(Reflections.class); /** * 調用Getter方法. * 支持多級,如:對象名.對象名.方法 */ static Object invokeGetter(Object obj, String propertyName) { Object object = obj; for (String name : StringUtils.split(propertyName, POINT)) { String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); object = invokeMethod(object, getterMethodName, new Class[]{}, new Object[]{}); } return object; } /** * 調用Setter方法, 僅匹配方法名。 * 支持多級,如:對象名.對象名.方法 */ static void invokeSetter(Object obj, String propertyName, Object value) { Object object = obj; String[] names = StringUtils.split(propertyName, "."); for (int i = 0; i < names.length; i++) { if (i < names.length - 1) { String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); object = invokeMethod(object, getterMethodName, new Class[]{}, new Object[]{}); } else { String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); invokeMethodByName(object, setterMethodName, new Object[]{value}); } } } /** * 直接讀取對象屬性值, 無視private/protected修飾符, 不經過getter函數. */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { logger.error("不可能拋出的異常{}", e.getMessage()); } return result; } /** * 直接設置對象屬性值, 無視private/protected修飾符, 不經過setter函數. */ public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } try { field.set(obj, value); } catch (IllegalAccessException e) { logger.error("不可能拋出的異常:{}", e.getMessage()); } } /** * 直接調用對象方法, 無視private/protected修飾符. * 用於一次性調用的情況,否則應使用getAccessibleMethod()函數獲得Method后反復調用. * 同時匹配方法名+參數類型, */ static Object invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes, final Object[] args) { Method method = getAccessibleMethod(obj, methodName, parameterTypes); if (method == null) { throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]"); } try { return method.invoke(obj, args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } /** * 直接調用對象方法, 無視private/protected修飾符, * 用於一次性調用的情況,否則應使用getAccessibleMethodByName()函數獲得Method后反復調用. * 只匹配函數名,如果有多個同名函數調用第一個。 */ public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) { Method method = getAccessibleMethodByName(obj, methodName); if (method == null) { throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]"); } try { return method.invoke(obj, args); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } /** * 循環向上轉型, 獲取對象的DeclaredField, 並強制設置為可訪問. * <p> * 如向上轉型到Object仍無法找到, 返回null. */ private static Field getAccessibleField(final Object obj, final String fieldName) { Validate.notNull(obj, "object can't be null"); Validate.notBlank(fieldName, "fieldName can't be blank"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) { try { Field field = superClass.getDeclaredField(fieldName); makeAccessible(field); return field; } catch (NoSuchFieldException e) {//NOSONAR // Field不在當前類定義,繼續向上轉型 } } return null; } /** * 循環向上轉型, 獲取對象的DeclaredMethod,並強制設置為可訪問. * 如向上轉型到Object仍無法找到, 返回null. * 匹配函數名+參數類型。 * <p> * 用於方法需要被多次調用的情況. 先使用本函數先取得Method,然后調用Method.invoke(Object obj, Object... args) */ private static Method getAccessibleMethod(final Object obj, final String methodName, final Class<?>... parameterTypes) { Validate.notNull(obj, "object can't be null"); Validate.notBlank(methodName, "methodName can't be blank"); for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { try { Method method = searchType.getDeclaredMethod(methodName, parameterTypes); makeAccessible(method); return method; } catch (NoSuchMethodException e) { // Method不在當前類定義,繼續向上轉型 } } return null; } /** * 循環向上轉型, 獲取對象的DeclaredMethod,並強制設置為可訪問. * 如向上轉型到Object仍無法找到, 返回null. * 只匹配函數名。 * <p> * 用於方法需要被多次調用的情況. 先使用本函數先取得Method,然后調用Method.invoke(Object obj, Object... args) */ private static Method getAccessibleMethodByName(final Object obj, final String methodName) { Validate.notNull(obj, "object can't be null"); Validate.notBlank(methodName, "methodName can't be blank"); for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) { Method[] methods = searchType.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { makeAccessible(method); return method; } } } return null; } /** * 改變private/protected的方法為public,盡量不調用實際改動的語句,避免JDK的SecurityManager抱怨。 */ private static void makeAccessible(Method method) { if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) { method.setAccessible(true); } } /** * 改變private/protected的成員變量為public,盡量不調用實際改動的語句,避免JDK的SecurityManager抱怨。 */ private static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier .isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } } /** * 通過反射, 獲得Class定義中聲明的泛型參數的類型, 注意泛型必須定義在父類處 * 如無法找到, 返回Object.class. * eg. * public UserDao extends HibernateDao<User> * * @param clazz The class to introspect * @return the first generic declaration, or Object.class if cannot be determined */ @SuppressWarnings("unchecked") public static <T> Class<T> getClassGenricType(final Class clazz) { return getClassGenricType(clazz, 0); } /** * 通過反射, 獲得Class定義中聲明的父類的泛型參數的類型. * 如無法找到, 返回Object.class. * <p> * 如public UserDao extends HibernateDao<User,Long> * * @param clazz clazz The class to introspect * @param index the Index of the generic ddeclaration,start from 0. * @return the index generic declaration, or Object.class if cannot be determined */ private static Class getClassGenricType(final Class clazz, final int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType"); return Object.class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index] instanceof Class)) { logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; } public static Class<?> getUserClass(Object instance) { Assert.notNull(instance, "Instance must not be null"); Class clazz = instance.getClass(); if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) { Class<?> superClass = clazz.getSuperclass(); if (superClass != null && !Object.class.equals(superClass)) { return superClass; } } return clazz; } /** * 將反射時的checked exception轉換為unchecked exception. */ private static RuntimeException convertReflectionExceptionToUnchecked(Exception e) { if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException || e instanceof NoSuchMethodException) { return new IllegalArgumentException(e); } else if (e instanceof InvocationTargetException) { return new RuntimeException(((InvocationTargetException) e).getTargetException()); } else if (e instanceof RuntimeException) { return (RuntimeException) e; } return new RuntimeException("Unexpected Checked Exception.", e); } }
-
測試
package cn.lisongyu.common.base; import cn.lisongyu.common.annotation.ExcelField; import cn.lisongyu.common.util.ExportExcel; import cn.lisongyu.common.util.ImportExcel; import lombok.*; import lombok.extern.slf4j.Slf4j; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; /** * @author lisongyu * @date 2019/09/23 */ @Data @Builder @AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor public class TestClass { @ExcelField(title = "編號", align = 2) private Integer id; @ExcelField(title = "姓名", align = 2, sort = 1) private String name; @ExcelField(title = "年齡", align = 2, sort = 2) private Integer age; @ExcelField(title = "薪水", align = 2, sort = 3) private Double salary; @ExcelField(title = "出生日期", align = 2, sort = 4) private String birthday; } @Slf4j class Test { public static void main(String[] args) throws Exception { List<TestClass> testClasses = new ArrayList<>(); for (int i = 1; i <= 10; i++) { TestClass testClass = TestClass.builder() .id(i) .name("測試" + i) .age(new Random().nextInt(100)) .salary(10000d) .birthday(new SimpleDateFormat("yyyy-MM-dd").format(new Date())) .build(); testClasses.add(testClass); } ExportExcel exportExcel = new ExportExcel("測試導出", TestClass.class).setDataList(testClasses); exportExcel.writeFile("E:\\1.xlsx"); exportExcel.dispose(); ImportExcel importExcel = new ImportExcel("E:\\1.xlsx", 1, 0); List<TestClass> list = importExcel.getDataList(TestClass.class); for (int i = 0; i < list.size(); i++) { TestClass excelModelTest = list.get(i); log.info(excelModelTest.toString()); } } }
-
結果
19:46:59.423 [main] DEBUG cn.lisongyu.common.util.ExportExcel - Initialize success. 19:46:59.813 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [1, 測試1, 0, 10000.0, 2019-09-24, ] 19:46:59.814 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [2, 測試2, 63, 10000.0, 2019-09-24, ] 19:46:59.815 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [3, 測試3, 18, 10000.0, 2019-09-24, ] 19:46:59.815 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [4, 測試4, 91, 10000.0, 2019-09-24, ] 19:46:59.816 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [5, 測試5, 1, 10000.0, 2019-09-24, ] 19:46:59.816 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [6, 測試6, 47, 10000.0, 2019-09-24, ] 19:46:59.817 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [7, 測試7, 19, 10000.0, 2019-09-24, ] 19:46:59.818 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [8, 測試8, 40, 10000.0, 2019-09-24, ] 19:46:59.818 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [9, 測試9, 72, 10000.0, 2019-09-24, ] 19:46:59.819 [main] INFO cn.lisongyu.common.util.ImportExcel - Read success: [10, 測試10, 51, 10000.0, 2019-09-24, ] 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=1, name=測試1, age=0, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=2, name=測試2, age=63, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=3, name=測試3, age=18, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=4, name=測試4, age=91, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=5, name=測試5, age=1, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=6, name=測試6, age=47, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=7, name=測試7, age=19, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=8, name=測試8, age=40, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=9, name=測試9, age=72, salary=10000.0, birthday=2019-09-24) 19:46:59.819 [main] INFO cn.lisongyu.common.base.Test - TestClass(id=10, name=測試10, age=51, salary=10000.0, birthday=2019-09-24)