一、需要導(dǎo)入的jar
1.commons-collections4-4.1.jar
2.poi-3.17-beta1.jar
3.poi-ooxml-3.17-beta1.jar
4.poi-ooxml-schemas-3.17-beta1.jar
5.xmlbeans-2.6.0.jar
二、主要api
1.import org.apache.poi.ss.usermodel.workbook,對應(yīng)excel文檔;
2.import org.apache.poi.hssf.usermodel.hssfworkbook,對應(yīng)xls格式的excel文檔;
3.import org.apache.poi.xssf.usermodel.xssfworkbook,對應(yīng)xlsx格式的excel文檔;
4.import org.apache.poi.ss.usermodel.sheet,對應(yīng)excel文檔中的一個sheet;
5.import org.apache.poi.ss.usermodel.row,對應(yīng)一個sheet中的一行;
6.import org.apache.poi.ss.usermodel.cell,對應(yīng)一個單元格。
三、代碼如下
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
|
package poi; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.linkedhashmap; import java.util.list; import java.util.map; import java.util.map.entry; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.dateutil; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbook; import org.apache.poi.xssf.usermodel.xssfworkbook; public class testpoi { public static void main(string[] args) { workbook wb = null ; sheet sheet = null ; row row = null ; list<map<string,string>> list = null ; string celldata = null ; string filepath = "d:\\test.xlsx" ; string columns[] = { "name" , "age" , "score" }; wb = readexcel(filepath); if (wb != null ){ //用來存放表中數(shù)據(jù) list = new arraylist<map<string,string>>(); //獲取第一個sheet sheet = wb.getsheetat( 0 ); //獲取最大行數(shù) int rownum = sheet.getphysicalnumberofrows(); //獲取第一行 row = sheet.getrow( 0 ); //獲取最大列數(shù) int colnum = row.getphysicalnumberofcells(); for ( int i = 1 ; i<rownum; i++) { map<string,string> map = new linkedhashmap<string,string>(); row = sheet.getrow(i); if (row != null ){ for ( int j= 0 ;j<colnum;j++){ celldata = (string) getcellformatvalue(row.getcell(j)); map.put(columns[j], celldata); } } else { break ; } list.add(map); } } //遍歷解析出來的list for (map<string,string> map : list) { for (entry<string,string> entry : map.entryset()) { system.out.print(entry.getkey()+ ":" +entry.getvalue()+ "," ); } system.out.println(); } } //讀取excel public static workbook readexcel(string filepath){ workbook wb = null ; if (filepath== null ){ return null ; } string extstring = filepath.substring(filepath.lastindexof( "." )); inputstream is = null ; try { is = new fileinputstream(filepath); if ( ".xls" .equals(extstring)){ return wb = new hssfworkbook(is); } else if ( ".xlsx" .equals(extstring)){ return wb = new xssfworkbook(is); } else { return wb = null ; } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return wb; } public static object getcellformatvalue(cell cell){ object cellvalue = null ; if (cell!= null ){ //判斷cell類型 switch (cell.getcelltype()){ case cell.cell_type_numeric:{ cellvalue = string.valueof(cell.getnumericcellvalue()); break ; } case cell.cell_type_formula:{ //判斷cell是否為日期格式 if (dateutil.iscelldateformatted(cell)){ //轉(zhuǎn)換為日期格式y(tǒng)yyy-mm-dd cellvalue = cell.getdatecellvalue(); } else { //數(shù)字 cellvalue = string.valueof(cell.getnumericcellvalue()); } break ; } case cell.cell_type_string:{ cellvalue = cell.getrichstringcellvalue().getstring(); break ; } default : cellvalue = "" ; } } else { cellvalue = "" ; } return cellvalue; } } |
四、運行結(jié)果
代碼運行前保證在d盤下有一個test.xlsx文檔,不然報文件找不到異常;excel文檔中的表頭要和代碼中的string columns[] = {"name","age","score"}
對應(yīng)起來。
總結(jié)
以上所述是小編給大家介紹的java解析excel的方法(xls、xlsx兩種格式),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/hhhshct/p/7255915.html