Java利用POI读取Excel
官网直接下载POI http://poi.apache.org/
1 package com.CommonUtil;2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9
10 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12 import org.apache.poi.ss.usermodel.Cell;
13 import org.apache.poi.ss.usermodel.DateUtil;
14 import org.apache.poi.ss.usermodel.FormulaEvaluator;
15 import org.apache.poi.ss.usermodel.Row;
16 import org.apache.poi.ss.usermodel.Sheet;
17 import org.apache.poi.ss.usermodel.Workbook;
18 import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
19 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
20
21 /**
22 * excel文件读取工具类,支持xls,xlsx两种格式
23 * @author Andrew
24 *
25 */
26 public class ExcelUtil {
27
28 /**
29 * excel文件读取指定列的数据
30 * @author Andrew
31 * @param excelPath 文件名
32 * @param args 需要查询的列号
33 * @return ArrayList<ArrayList<String>> 二维字符串数组
34 * @throws IOException
35 */
36 @SuppressWarnings({ "unused" })
37 public ArrayList<ArrayList<String>> excelReader(String excelPath,int ... args) throws IOException {
38 // 创建excel工作簿对象
39 Workbook workbook = null;
40 FormulaEvaluator formulaEvaluator = null;
41 // 读取目标文件
42 File excelFile = new File(excelPath);
43 InputStream is = new FileInputStream(excelFile);
44 // 判断文件是xlsx还是xls
45 if (excelFile.getName().endsWith("xlsx")) {
46 workbook = new XSSFWorkbook(is);
47 formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
48 }else {
49 workbook = new HSSFWorkbook(is);
50 formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
51 }
52
53 //判断excel文件打开是否正确
54 if(workbook == null){
55 System.err.println("未读取到内容,请检查路径!");
56 return null;
57 }
58 //创建二维数组,储存excel行列数据
59 ArrayList<ArrayList<String>> als = new ArrayList<ArrayList<String>>();
60 //遍历工作簿中的sheet
61 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
62 Sheet sheet = workbook.getSheetAt(numSheet);
63 //当前sheet页面为空,继续遍历
64 if (sheet == null) {
65 continue;
66 }
67 // 对于每个sheet,读取其中的每一行
68 for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
69 Row row = sheet.getRow(rowNum);
70 if (row == null) {
71 continue;
72 }
73 ArrayList<String> al = new ArrayList<String>();
74 // 遍历每一行的每一列
75 for(int columnNum = 0 ; columnNum < args.length ; columnNum++){
76 Cell cell = row.getCell(args[columnNum]);
77 al.add(getValue(cell, formulaEvaluator));
78 }
79 als.add(al);
80 }
81 }
82 is.close();
83 return als;
84 }
85
86 /**
87 * excel文件读取全部信息
88 * @author Andrew
89 * @param excelPath 文件名
90 * @return ArrayList<ArrayList<String>> 二维字符串数组
91 * @throws IOException
92 */
93 @SuppressWarnings({ "unused" })
94 public ArrayList<ArrayList<String>> excelReader(String excelPath) throws IOException {
95 // 创建excel工作簿对象
96 Workbook workbook = null;
97 FormulaEvaluator formulaEvaluator = null;
98 // 读取目标文件
99 File excelFile = new File(excelPath);
100 InputStream is = new FileInputStream(excelFile);
101 // 判断文件是xlsx还是xls
102 if (excelFile.getName().endsWith("xlsx")) {
103 workbook = new XSSFWorkbook(is);
104 formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
105 }else {
106 workbook = new HSSFWorkbook(is);
107 formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
108 }
109
110 //判断excel文件打开是否正确
111 if(workbook == null){
112 System.err.println("未读取到内容,请检查路径!");
113 return null;
114 }
115 //创建二维数组,储存excel行列数据
116 ArrayList<ArrayList<String>> als = new ArrayList<ArrayList<String>>();
117 //遍历工作簿中的sheet
118 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
119 Sheet sheet = workbook.getSheetAt(numSheet);
120 //当前sheet页面为空,继续遍历
121 if (sheet == null) {
122 continue;
123 }
124 // 对于每个sheet,读取其中的每一行
125 for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
126 Row row = sheet.getRow(rowNum);
127 if (row == null) {
128 continue;
129 }
130 // 遍历每一行的每一列
131 ArrayList<String> al = new ArrayList<String>();
132 for(int columnNum = 0 ; columnNum < row.getLastCellNum(); columnNum++){
133 Cell cell = row.getCell(columnNum);
134 al.add(getValue(cell, formulaEvaluator));
135 }
136 als.add(al);
137 }
138 }
139 is.close();
140 return als;
141 }
142
143 /**
144 * excel文件的数据读取,包括后缀为xls,xlsx
145 * @param xssfRow
146 * @return
147 */
148 @SuppressWarnings("deprecation")
149 private static String getValue(Cell cell, FormulaEvaluator formulaEvaluator) {
150 if(cell==null){
151 return null;
152 }
153 switch (cell.getCellType()) {
154 case Cell.CELL_TYPE_STRING:
155 return cell.getRichStringCellValue().getString();
156 case Cell.CELL_TYPE_NUMERIC:
157 // 判断是日期时间类型还是数值类型
158 if (DateUtil.isCellDateFormatted(cell)) {
159 short format = cell.getCellStyle().getDataFormat();
160 SimpleDateFormat sdf = null;
161 /* 所有日期格式都可以通过getDataFormat()值来判断
162 * yyyy-MM-dd----- 14
163 * yyyy年m月d日----- 31
164 * yyyy年m月--------57
165 * m月d日 --------- 58
166 * HH:mm---------- 20
167 * h时mm分 --------- 32
168 */
169 if(format == 14 || format == 31 || format == 57 || format == 58){
170 //日期
171 sdf = new SimpleDateFormat("yyyy-MM-dd");
172 }else if (format == 20 || format == 32) {
173 //时间
174 sdf = new SimpleDateFormat("HH:mm");
175 }
176 return sdf.format(cell.getDateCellValue());
177 } else {
178 // 对整数进行判断处理
179 double cur = cell.getNumericCellValue();
180 long longVal = Math.round(cur);
181 Object inputValue = null;
182 if(Double.parseDouble(longVal + ".0") == cur) {
183 inputValue = longVal;
184 }
185 else {
186 inputValue = cur;
187 }
188 return String.valueOf(inputValue);
189 }
190 case Cell.CELL_TYPE_BOOLEAN:
191 return String.valueOf(cell.getBooleanCellValue());
192 case Cell.CELL_TYPE_FORMULA:
193 //对公式进行处理,返回公式计算后的值,使用cell.getCellFormula()只会返回公式
194 return String.valueOf(formulaEvaluator.evaluate(cell).getNumberValue());
195 //Cell.CELL_TYPE_BLANK || Cell.CELL_TYPE_ERROR
196 default:
197 return null;
198 }
199 }
200 }
以上是 Java利用POI读取Excel 的全部内容, 来源链接: utcz.com/z/391899.html