項目需要解析Excel文檔獲取數(shù)據(jù),就在網(wǎng)上找了一些資料,結(jié)合自己這次使用,寫下心得:
1、maven項目需加入如下依賴:
1
2
3
4
5
6
7
8
9
10
|
< dependency > < artifactId >poi</ artifactId > < version >3.10-FINAL</ version > </ dependency > < dependency > < groupId >org.apache.poi</ groupId > < artifactId >poi-ooxml</ artifactId > < version >3.10-FINAL</ version > </ dependency > |
直接上測試類,類里有完善的注釋:
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
|
package shindo.Java; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtil { public static void main(String[] args) { String path = "D:\\IDE\\workspace-Neon\\Java\\src\\refund.xls" ; try { List<List<String>> result = new ExcelUtil().readXls(path); System.out.println(result.size()); for ( int i = 0 ; i < result.size(); i++) { List<String> model = result.get(i); System.out.println( "orderNum:" + model.get( 0 ) + "--> orderAmount:" + model.get( 1 )); } } catch (Exception e) { e.printStackTrace(); } } /** * * @Title: readXls * @Description: 處理xls文件 * @param @param path * @param @return * @param @throws Exception 設(shè)定文件 * @return List<List<String>> 返回類型 * @throws * * 從代碼不難發(fā)現(xiàn)其處理邏輯: * 1.先用InputStream獲取excel文件的io流 * 2.然后穿件一個內(nèi)存中的excel文件HSSFWorkbook類型對象,這個對象表示了整個excel文件。 * 3.對這個excel文件的每頁做循環(huán)處理 * 4.對每頁中每行做循環(huán)處理 * 5.對每行中的每個單元格做處理,獲取這個單元格的值 * 6.把這行的結(jié)果添加到一個List數(shù)組中 * 7.把每行的結(jié)果添加到最后的總結(jié)果中 * 8.解析完以后就獲取了一個List<List<String>>類型的對象了 * */ private List<List<String>> readXls(String path) throws Exception { InputStream is = new FileInputStream(path); // HSSFWorkbook 標識整個excel HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); List<List<String>> result = new ArrayList<List<String>>(); int size = hssfWorkbook.getNumberOfSheets(); // 循環(huán)每一頁,并處理當前循環(huán)頁 for ( int numSheet = 0 ; numSheet < size; numSheet++) { // HSSFSheet 標識某一頁 HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null ) { continue ; } // 處理當前頁,循環(huán)讀取每一行 for ( int rowNum = 1 ; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { // HSSFRow表示行 HSSFRow hssfRow = hssfSheet.getRow(rowNum); int minColIx = hssfRow.getFirstCellNum(); int maxColIx = hssfRow.getLastCellNum(); List<String> rowList = new ArrayList<String>(); // 遍歷改行,獲取處理每個cell元素 for ( int colIx = minColIx; colIx < maxColIx; colIx++) { // HSSFCell 表示單元格 HSSFCell cell = hssfRow.getCell(colIx); if (cell == null ) { continue ; } rowList.add(getStringVal(cell)); } result.add(rowList); } } return result; } /** * * @Title: readXlsx * @Description: 處理Xlsx文件 * @param @param path * @param @return * @param @throws Exception 設(shè)定文件 * @return List<List<String>> 返回類型 * @throws */ private List<List<String>> readXlsx(String path) throws Exception { InputStream is = new FileInputStream(path); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); List<List<String>> result = new ArrayList<List<String>>(); // 循環(huán)每一頁,并處理當前循環(huán)頁 for (XSSFSheet xssfSheet : xssfWorkbook) { if (xssfSheet == null ) { continue ; } // 處理當前頁,循環(huán)讀取每一行 for ( int rowNum = 1 ; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); int minColIx = xssfRow.getFirstCellNum(); int maxColIx = xssfRow.getLastCellNum(); List<String> rowList = new ArrayList<String>(); for ( int colIx = minColIx; colIx < maxColIx; colIx++) { XSSFCell cell = xssfRow.getCell(colIx); if (cell == null ) { continue ; } rowList.add(cell.toString()); } result.add(rowList); } } return result; } // 存在的問題 /* * 其實有時候我們希望得到的數(shù)據(jù)就是excel中的數(shù)據(jù),可是最后發(fā)現(xiàn)結(jié)果不理想 * 如果你的excel中的數(shù)據(jù)是數(shù)字,你會發(fā)現(xiàn)Java中對應的變成了科學計數(shù)法。 * 所以在獲取值的時候就要做一些特殊處理來保證得到自己想要的結(jié)果 * 網(wǎng)上的做法是對于數(shù)值型的數(shù)據(jù)格式化,獲取自己想要的結(jié)果。 * 下面提供另外一種方法,在此之前,我們先看一下poi中對于toString()方法: * * 該方法是poi的方法,從源碼中我們可以發(fā)現(xiàn),該處理流程是: * 1.獲取單元格的類型 * 2.根據(jù)類型格式化數(shù)據(jù)并輸出。這樣就產(chǎn)生了很多不是我們想要的 * 故對這個方法做一個改造。 */ /*public String toString(){ switch(getCellType()){ case CELL_TYPE_BLANK: return ""; case CELL_TYPE_BOOLEAN: return getBooleanCellValue() ? "TRUE" : "FALSE"; case CELL_TYPE_ERROR: return ErrorEval.getText(getErrorCellValue()); case CELL_TYPE_FORMULA: return getCellFormula(); case CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(this)){ DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy") return sdf.format(getDateCellValue()); } return getNumericCellValue() + ""; case CELL_TYPE_STRING: return getRichStringCellValue().toString(); default : return "Unknown Cell Type:" + getCellType(); } }*/ /** * 改造poi默認的toString()方法如下 * @Title: getStringVal * @Description: 1.對于不熟悉的類型,或者為空則返回""控制串 * 2.如果是數(shù)字,則修改單元格類型為String,然后返回String,這樣就保證數(shù)字不被格式化了 * @param @param cell * @param @return 設(shè)定文件 * @return String 返回類型 * @throws */ public static String getStringVal(HSSFCell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue() ? "TRUE" : "FALSE" ; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: cell.setCellType(Cell.CELL_TYPE_STRING); return cell.getStringCellValue(); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default : return "" ; } } } |
總結(jié)
以上就是本文關(guān)于Java使用poi包讀取Excel文檔代碼分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/yan456jie/article/details/77283027