2343图2 发表于 2016-12-28 10:50:08

使用Apache POI 读取Excel文件

生活中用到用到Excel文件的情况很多,什么商品进货单,产品维修单,餐厅的营业额等等。作为程序员,我们该如何读取Excel文件,获取我们想要的资源呢。本篇将讲解如何使用Apache POI读取Excel文件。
    准备工作:
    1)Apache POI 开发jar包

    2)Excel资源文件,包括Excel2003,Excel2007这两种版本分别对应xls、xlsx文件。

    本篇已经为您做好准备工作,请点击此处,下载资源文件,你也可以浏览Apace POI官网了解更多详细信息。

    简要流程:

    获取WorkBook实例- ->获取Sheet实例- ->获取Row实例- ->获取Cell实例- ->取得需要值- ->显示数据;
    因为不愿意写重复的代码,本篇用到了java反射的一些知识,如有读者不认同,请查看本文中给出官方链接进行学习。
   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.test.excel.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/*Excel文件读取工具类*/
public class ExcelReadUtil {
    /* 以键值对形式存储数据,这里使用LinkHashMap(可以记录添加顺序),若需要自定义比较器可以使用HashMap、HashTable */
    private LinkedHashMap<String, List<String>> map = new LinkedHashMap<String, List<String>>();
    /* 使用接口Workbook指向HSSFWorkbook子类或者XSSFWorkbook子类 */
    private Workbook workbook;

    /**
   *
   * @param filePath文件路径
   * @param rowIndex起始行
   * @param colIndex起始列
   * @throws FileNotFoundException
   * @throws IOException
   */
    public void readExcel(String filePath, int rowIndex, int colIndex) throws FileNotFoundException, IOException {
      try {
            // 针对与Excel 2007及以上版本
            workbook = new XSSFWorkbook(new FileInputStream(new File(filePath)));
      } catch (Exception e) {
            // 针对Excel 2007一下版本
            workbook = new HSSFWorkbook(new FileInputStream(new File(filePath)));
      } finally {
            // 用于Method无参数的发射调用
            Class<?>[] noC = new Class<?>[] {};
            // 用于Method参数为Int的发射调用
            Class<?>[] intC = new Class<?>[] { int.class };
            // Method反射调用需要的空参数
            Object[] noO = new Object[] {};
            // 获取Sheet(Object是所有类直接或者间接父类,用Object指代反射生成的对象)
            Object sheet = ReflectMethodUtil.getObjectByAll(workbook, "getSheetAt", new Object[] { 0 }, intC);
            // 获取当前sheet的行数
            int rows = (int) ReflectMethodUtil.getObjectByAll(sheet, "getPhysicalNumberOfRows", noO, noC);
            // 用于指向Row对象,Cell对象
            Object row = null, col = null;
            // 获取Row、Cell的反射方法
            Method getRowMethod = null, getCellMethod = null;
            // 用于存储方法名
            String tmn = "";
            // 遍历Rows,获取Row
            for (int i = rowIndex; i < rows; i++) {
                if (getRowMethod == null) {
                  // 使用Method.invoke()方法获取Row对象,并设置getRowMethod,以便于二次调用
                  row = ReflectMethodUtil.getObjectByAll(sheet, "getRow", new Object[] { i }, intC);
                  getRowMethod = ReflectMethodUtil.util.getMethod();
                } else {
                  // getRowMethod不为空,直接使用Method.invoke()获取Row对象
                  row = ReflectMethodUtil.getObjectWithoutPramTypes(getRowMethod, sheet, new Object[] { i });
                }
                // 若row为空,则跳过本次循环
                if (row == null) {
                  continue;
                }
                // row不为空,获取Row的列数
                int cols = (int) ReflectMethodUtil.getObjectByAll(row, "getPhysicalNumberOfCells", noO, noC);
                // 用于存储获取到的值
                List<String> list = new ArrayList<String>();
                // 遍历Cell
                for (int j = colIndex; j < cols; j++) {
                  String value = "";
                  if (getCellMethod == null) {
                        col = ReflectMethodUtil.getObjectByAll(row, "getCell", new Object[] { j }, intC);
                        getCellMethod = ReflectMethodUtil.util.getMethod();
                  } else {
                        col = ReflectMethodUtil.getObjectWithoutPramTypes(getCellMethod, row, new Object[] { j });
                  }
                  if (col == null) {
                        continue;
                  }
                  CellType type = (CellType) ReflectMethodUtil.getObjectByAll(col, "getCellTypeEnum", noO, noC);
                  // 获取Cell的值,并转成String
                  switch (type) {
                  case FORMULA:
                  case NUMERIC:
                        tmn = "getNumericCellValue";
                        value = (int) ((double) ReflectMethodUtil.getObjectByAll(col, tmn, noO, noC)) + "";
                        break;
                  case STRING:
                        tmn = "getStringCellValue";
                        value = (String) ReflectMethodUtil.getObjectByAll(col, tmn, noO, noC);
                        break;
                  default:
                        break;
                  }
                  // 若value为空,跳过本次循环,不添加进List
                  if (value.equals("")) {
                        continue;
                  }
                  // 替换掉字符串中的换行符
                  value = value.replaceAll("\n", "");
                  list.add(value);
                }
                // 若list的长度为零,则跳过本次循环,不将list添加进LInkHashMap
                if (list.size() < 1) {
                  continue;
                }
                map.put(list.get(0), list);
            }
      }

    }

    public static void main(String[] args) throws Exception {
      ExcelReadUtil util = new ExcelReadUtil();
      // util.readExcel("C:\\Users\\linYang\\Desktop\\20161223_095309.xlsx",
      // 3, 1);
      util.readExcel("C:\\Users\\linYang\\Desktop\\Excel练习文档.xls", 0, 0);
      for (Map.Entry<String, List<String>> entry : util.map.entrySet()) {
            List<String> list = entry.getValue();
            for (int i = 0, length = list.size(); i < length; i++) {
                System.out.print(list.get(i) + "\t");
                if (i == length - 1) {
                  System.out.println();
                }
            }
      }
    }
}

/* 方法反射工具类 */
class ReflectMethodUtil {
    private Method method;// Method对象
    private Class<?> clazz;// 当前需要进行操作的类类型
    private Object object;// Method.invoke()参数

    public static ReflectMethodUtil util = null;

    public Method getMethod() {
      return method;
    }

    public void setMethod(Method method) {
      this.method = method;
    }

    public Class<?> getClazz() {
      return clazz;
    }

    public void setClazz(Class<?> clazz) {
      this.clazz = clazz;
    }

    public Object getObject() {
      return object;
    }

    public void setObject(Object object) {
      this.object = object;
    }

    /**
   * @param clazz
   * @param object
   */
    public ReflectMethodUtil(Class<?> clazz, Object object) {
      super();
      this.clazz = clazz;
      this.object = object;
    }

    /**
   *
   * @param methodName需要获取的Method的方法名
   * @param parameterTypesMethod参数的类型数组
   * @return
   */
    public Method getMethodByName(String methodName, Class<?>... parameterTypes) {
      try {
            method = clazz.getMethod(methodName, parameterTypes);
      } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
      }

      return method;
    }

    /**
   *
   * @param paramters通过Object数组获取到Object
   * @return
   */
    public Object getObjectByMethodAndParamter(Object... paramters) {
      try {
            return method.invoke(this.object, paramters);
      } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
      }
      return null;
    }

    /**
   * 获取Object
   *
   * @param obj
   * @param methodName
   * @param paramters
   * @param parameterTypes
   * @return
   */
    public static Object getObjectByAll(Object obj, String methodName, Object[] paramters, Class<?>... parameterTypes) {
      util = new ReflectMethodUtil(obj.getClass(), obj);
      util.method = util.getMethodByName(methodName, parameterTypes);
      return util.getObjectByMethodAndParamter(paramters);
    }

    /**
   * 获取Object
   *
   * @param method
   * @param object
   * @param paramters
   * @return
   */
    public static Object getObjectWithoutPramTypes(Method method, Object object, Object[] paramters) {
      try {
            return method.invoke(object, paramters);
      } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
      }
      return null;
    }

}






页: [1]
查看完整版本: 使用Apache POI 读取Excel文件