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