EXCEL导入(反射)

  1 import java.io.File;
  2 import java.io.FileInputStream;
  3 import java.io.FileNotFoundException;
  4 import java.io.IOException;
  5 import java.lang.reflect.Field;
  6 import java.lang.reflect.Method;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import org.apache.poi.hssf.usermodel.HSSFRow;
 11 import org.apache.poi.hssf.usermodel.HSSFSheet;
 12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 13 
 14 import com.jeffy.bean.Indexmanage;
 15 
 16 /**
 17  * 读取excel的公用方法
 18  * @author Jeffy
 19  *
 20  */
 21 public class ExcelManage {
 22     private HSSFWorkbook workbook;  
 23     
 24     public ExcelManage(String fileDir) {  
 25         File file = new File(fileDir);  
 26         try {  
 27             workbook = new HSSFWorkbook(new FileInputStream(file));  
 28         } catch (FileNotFoundException e) {  
 29             e.printStackTrace();  
 30         } catch (IOException e) {  
 31             e.printStackTrace();  
 32         }  
 33     }  
 34   
 35     /** 
 36      * 读取excel表中的数据. 
 37      *  
 38      * @param sheetName 
 39      *            表格索引(EXCEL 是多表文档,所以需要输入表索引号,如sheet1) 
 40      */  
 41     public List readFromExcel(String sheetName, Object object) {  
 42   
 43         List result = new ArrayList();  
 44         // 获取该对象的class对象  
 45         Class class_ = object.getClass();  
 46         // 获得该类的所有属性  
 47         Field[] fields = class_.getDeclaredFields();  
 48   
 49         // 读取excel数据  
 50         // 获得指定的excel表  
 51         HSSFSheet sheet = workbook.getSheet(sheetName);  
 52         // 获取表格的总行数  
 53         int rowCount = sheet.getLastRowNum() + 1; // 需要加一  
 54         if (rowCount < 1) {  
 55             return result;  
 56         }  
 57         // 获取表头的列数  
 58         int columnCount = sheet.getRow(0).getLastCellNum();  
 59         // 读取表头信息,确定需要用的方法名---set方法  
 60         // 用于存储方法名  
 61         String[] methodNames = new String[columnCount]; // 表头列数即为需要的set方法个数  
 62         // 用于存储属性类型  
 63         String[] fieldTypes = new String[columnCount];  
 64         // 获得表头行对象  
 65         HSSFRow titleRow = sheet.getRow(0);  
 66         // 遍历  
 67         for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { // 遍历表头列  
 68             String data = titleRow.getCell(columnIndex).toString(); // 某一列的内容  
 69             String Udata = Character.toUpperCase(data.charAt(0))  
 70                     + data.substring(1, data.length()); // 使其首字母大写  
 71             methodNames[columnIndex] = "set" + Udata;  
 72             for (int i = 0; i < fields.length; i++) { // 遍历属性数组  
 73                 if (data.equals(fields[i].getName())) { // 属性与表头相等  
 74                     fieldTypes[columnIndex] = fields[i].getType().getName(); // 将属性类型放到数组中  
 75                 }  
 76             }  
 77         }  
 78         // 逐行读取数据 从1开始 忽略表头  
 79         for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {  
 80             // 获得行对象  
 81             HSSFRow row = sheet.getRow(rowIndex);  
 82             if (row != null) {  
 83                 Object obj = null;  
 84                 // 实例化该泛型类的对象一个对象  
 85                 try {  
 86                     obj = class_.newInstance();  
 87                 } catch (Exception e1) {  
 88                     e1.printStackTrace();  
 89                 }  
 90   
 91                 // 获得本行中各单元格中的数据  
 92                 for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {  
 93                     String data = row.getCell(columnIndex).toString();  
 94                     // 获取要调用方法的方法名  
 95                     String methodName = methodNames[columnIndex];  
 96                     Method method = null;  
 97                     try {  
 98                         // 这部分可自己扩展  
 99                         if (fieldTypes[columnIndex].equals("java.lang.String")) {  
100                             method = class_.getDeclaredMethod(methodName,  
101                                     String.class); // 设置要执行的方法--set方法参数为String  
102                             method.invoke(obj, data); // 执行该方法  
103                         } else if (fieldTypes[columnIndex].equals("int")) {  
104                             method = class_.getDeclaredMethod(methodName,  
105                                     int.class); // 设置要执行的方法--set方法参数为int  
106                             double data_double = Double.parseDouble(data);  
107                             int data_int = (int) data_double;  
108                             method.invoke(obj, data_int); // 执行该方法  
109                         }  
110                     } catch (Exception e) {  
111                         e.printStackTrace();  
112                     }  
113                 }  
114                 result.add(obj);  
115             }  
116         }  
117         return result;  
118     }  
119 
120     /**
121      * 读取EXCEL中的数据
122      * @param path EXCEL路径
123      * @param sheetName sheet页名称
124      * @param className 要返回的list的类
125      * @return 传入类的list
126      */
127     public static List getDataFromExcel(String path, String sheetName,Class<? extends Object> className) {  
128         ExcelManage em = new ExcelManage(path);  
129         Object obj = null;
130         try {
131             obj = className.newInstance();
132         } catch (InstantiationException e) {
133             // TODO Auto-generated catch block
134             e.printStackTrace();
135         } catch (IllegalAccessException e) {
136             // TODO Auto-generated catch block
137             e.printStackTrace();
138         }  
139         return em.readFromExcel(sheetName, obj);
140     }  
141     
142     public static void main(String[] args) {  
143         getDataFromExcel("d:/test2.xls", "sheet1", Indexmanage.class);
144     }  
145 }
EXCEL通用类

maven依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>
原文地址:https://www.cnblogs.com/superJF/p/6110258.html