一、定義
apache poi是apache軟件基金會(huì)的開(kāi)放源碼函式庫(kù),poi提供api給java程序?qū)icrosoft office格式檔案讀和寫(xiě)的功能。
二、所需jar包:
三、簡(jiǎn)單的一個(gè)讀取excel的demo
1、讀取文件方法
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
|
/** * 讀取出filepath中的所有數(shù)據(jù)信息 * @param filepath excel文件的絕對(duì)路徑 * */ public static void getdatafromexcel(string filepath) { //string filepath = "e:\\123.xlsx"; //判斷是否為excel類型文件 if (!filepath.endswith( ".xls" )&&!filepath.endswith( ".xlsx" )) { system.out.println( "文件不是excel類型" ); } fileinputstream fis = null ; workbook wookbook = null ; try { //獲取一個(gè)絕對(duì)地址的流 fis = new fileinputstream(filepath); } catch (exception e) { e.printstacktrace(); } try { //2003版本的excel,用.xls結(jié)尾 wookbook = new hssfworkbook(fis); //得到工作簿 } catch (exception ex) { //ex.printstacktrace(); try { //2007版本的excel,用.xlsx結(jié)尾 wookbook = new xssfworkbook(fis); //得到工作簿 } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } //得到一個(gè)工作表 sheet sheet = wookbook.getsheetat( 0 ); //獲得表頭 row rowhead = sheet.getrow( 0 ); //判斷表頭是否正確 if (rowhead.getphysicalnumberofcells() != 3 ) { system.out.println( "表頭的數(shù)量不對(duì)!" ); } //獲得數(shù)據(jù)的總行數(shù) int totalrownum = sheet.getlastrownum(); //要獲得屬性 string name = "" ; int latitude = 0 ; //獲得所有數(shù)據(jù) for ( int i = 1 ; i <= totalrownum ; i++) { //獲得第i行對(duì)象 row row = sheet.getrow(i); //獲得獲得第i行第0列的 string類型對(duì)象 cell cell = row.getcell(( short ) 0 ); name = cell.getstringcellvalue().tostring(); //獲得一個(gè)數(shù)字類型的數(shù)據(jù) cell = row.getcell(( short ) 1 ); latitude = ( int ) cell.getnumericcellvalue(); system.out.println( "名字:" +name+ ",經(jīng)緯度:" +latitude); } } |
2、測(cè)試
1
2
3
4
|
public static void main(string[] args) { getdatafromexcel( "e:" + file.separator + "123.xlsx" ); } |
3、原始數(shù)據(jù)
4、結(jié)果
1
2
3
4
5
6
7
8
9
10
11
|
名字:a1,經(jīng)緯度: 1 名字:a2,經(jīng)緯度: 2 名字:a3,經(jīng)緯度: 3 名字:a4,經(jīng)緯度: 4 名字:a5,經(jīng)緯度: 5 名字:a6,經(jīng)緯度: 6 名字:a7,經(jīng)緯度: 7 名字:a8,經(jīng)緯度: 8 名字:a9,經(jīng)緯度: 9 名字:a10,經(jīng)緯度: 10 名字:a11,經(jīng)緯度: 11 |
四、注意事項(xiàng)
1、運(yùn)用多態(tài),excel主要有.xls結(jié)尾(2003版本)和. xlsx(2007版本)兩種類型結(jié)尾的文件,分別需要用hssfworkbook對(duì)象對(duì).xls文件進(jìn)行讀取,用xssfworkbook對(duì)象對(duì).xlsx文件進(jìn)行讀取,直接使用他們共同的父類workbook進(jìn)行初始化對(duì)象有利于代碼的易用性。
2、通過(guò)流的方式初始化工作簿對(duì)象(workbook),可以通過(guò)new xssfworkbook(文件絕對(duì)路徑)和new xssfworkbook(輸入流)兩種方式初始化對(duì)象,但是假如我們只是通過(guò)修改.xls文件的后綴名為.xlsx,這樣子當(dāng)我們用new xssfworkbook(文件絕對(duì)路徑)來(lái)讀取文件的時(shí)候就會(huì)報(bào)錯(cuò),因?yàn)樗旧砭筒皇且粋€(gè)2007版本的excel類型的文件,讀取會(huì)報(bào)錯(cuò);假如我們是通過(guò)流的方式的話,可以避免這種情況,我們即使你修改了文件的后綴名,我們依然在初始化的時(shí)候能獲取到該對(duì)象是.xls類型文件,使用hssfworkbook對(duì)象進(jìn)行處理,即能得出正確的結(jié)果。
五、增強(qiáng)版
添加了判斷表頭是否符合規(guī)范,允許表頭對(duì)象的位置不同。進(jìn)行了一定的解耦合。
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
|
/** * * @param cell 一個(gè)單元格的對(duì)象 * @return 返回該單元格相應(yīng)的類型的值 */ public static object getrighttypecell(cell cell){ object object = null ; switch (cell.getcelltype()) { case cell.cell_type_string : { object=cell.getstringcellvalue(); break ; } case cell.cell_type_numeric : { cell.setcelltype(cell.cell_type_numeric); object=cell.getnumericcellvalue(); break ; } case cell.cell_type_formula : { cell.setcelltype(cell.cell_type_numeric); object=cell.getnumericcellvalue(); break ; } case cell.cell_type_blank : { cell.setcelltype(cell.cell_type_blank); object=cell.getstringcellvalue(); break ; } } return object; } |
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
|
/** * 讀取出filepath中的所有數(shù)據(jù)信息 * @param filepath excel文件的絕對(duì)路徑 * */ public static void getdatafromexcel2(string filepath) { list<map<string,integer>> list = new arraylist<map<string, integer>>(); //判斷是否為excel類型文件 if (!filepath.endswith( ".xls" )&&!filepath.endswith( ".xlsx" )) { system.out.println( "文件不是excel類型" ); } fileinputstream fis = null ; workbook wookbook = null ; int flag = 0 ; try { //獲取一個(gè)絕對(duì)地址的流 fis = new fileinputstream(filepath); } catch (exception e) { e.printstacktrace(); } try { //2003版本的excel,用.xls結(jié)尾 wookbook = new hssfworkbook(fis); //得到工作簿 } catch (exception ex) { //ex.printstacktrace(); try { //2007版本的excel,用.xlsx結(jié)尾 wookbook = new xssfworkbook(filepath); //得到工作簿 } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } //得到一個(gè)工作表 sheet sheet = wookbook.getsheetat( 0 ); //獲得表頭 row rowhead = sheet.getrow( 0 ); //根據(jù)不同的data放置不同的表頭 map<object,integer> headmap = new hashmap<object, integer>(); //判斷表頭是否合格 ------------------------這里看你有多少列 if (rowhead.getphysicalnumberofcells() != 2 ) { system.out.println( "表頭列數(shù)與要導(dǎo)入的數(shù)據(jù)庫(kù)不對(duì)應(yīng)" ); } try { //----------------這里根據(jù)你的表格有多少列 while (flag < 2 ) { cell cell = rowhead.getcell(flag); if (getrighttypecell(cell).tostring().equals( "基站名" )) { headmap.put( "jizhan" , flag); } if (getrighttypecell(cell).tostring().equals( "經(jīng)緯度" )) { headmap.put( "jingweidu" , flag); } flag++; } } catch (exception e) { e.printstacktrace(); system.out.println( "表頭不合規(guī)范,請(qǐng)修改后重新導(dǎo)入" ); } //獲得數(shù)據(jù)的總行數(shù) int totalrownum = sheet.getlastrownum(); //要獲得屬性 string name = "" ; double latitude = 0 ; if ( 0 == totalrownum) { system.out.println( "excel內(nèi)沒(méi)有數(shù)據(jù)!" ); } cell cell_1 = null ,cell_2 = null ; //獲得所有數(shù)據(jù) for ( int i = 1 ; i <= totalrownum ; i++) { //獲得第i行對(duì)象 row row = sheet.getrow(i); try { cell_1 = row.getcell(headmap.get( "jizhan" )); cell_2 = row.getcell(headmap.get( "jingweidu" )); } catch (exception e) { e.printstacktrace(); system.out.println( "獲取單元格錯(cuò)誤" ); } try { //基站 name = (string) getrighttypecell(cell_1); //經(jīng)緯度 latitude = ( double ) getrighttypecell(cell_2); } catch (classcastexception e) { e.printstacktrace(); system.out.println( "數(shù)據(jù)不全是數(shù)字或全部是文字!" ); } system.out.println( "名字:" +name+ ",經(jīng)緯度:" +latitude); } } |
異常情況:
應(yīng)將下面這段代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
try { //2003版本的excel,用.xls結(jié)尾 wookbook = new hssfworkbook(fis); //得到工作簿 } catch (exception ex) { //ex.printstacktrace(); try { //2007版本的excel,用.xlsx結(jié)尾 wookbook = new xssfworkbook(fis); //得到工作簿 } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } |
改為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
try { //2003版本的excel,用.xls結(jié)尾 wookbook = new hssfworkbook(fis); //得到工作簿 } catch (exception ex) { //ex.printstacktrace(); try { //這里需要重新獲取流對(duì)象,因?yàn)榍懊娴漠惓?dǎo)致了流的關(guān)閉——加了這一行 fis = new fileinputstream(filepath); //2007版本的excel,用.xlsx結(jié)尾 wookbook = new xssfworkbook(filepath); //得到工作簿 } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } |
解析:因?yàn)榍懊娈惓?dǎo)致了流的關(guān)閉,所以需要重新創(chuàng)建一個(gè)流對(duì)象。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。