使用Windows操作系統(tǒng)的朋友對(duì)Excel(電子表格)一定不會(huì)陌生,但是要使用Java語(yǔ)言來(lái)操縱Excel文件并不是一件容易的事。在Web應(yīng)用日益盛行的今天,通過(guò)Web來(lái)操作Excel文件的需求越來(lái)越強(qiáng)烈,目前較為流行的操作是在 JSP或Servlet 中創(chuàng)建一個(gè)CSV (comma separated values)文件,并將這個(gè)文件以MIME,text/csv類(lèi)型返回給瀏覽器,接著瀏覽器調(diào)用Excel并且顯示CSV文件。這樣只是說(shuō)可以訪問(wèn)到Excel文件,但是還不能真正的操縱Excel 文件,本文將給大家一個(gè)驚喜,向大家介紹一個(gè)開(kāi)放源碼項(xiàng)目 Java Excel API,使用它大家就可以方便地操縱Excel文件了。
總結(jié)了兩種主要文件格式操作實(shí)例
一:java 對(duì).csv 文件格式的操作
1
/*
2
* Created on 2007-11-21
3
*
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
6
*/
7
package com.job5156.xlstodb;
8
9
import java.io.BufferedReader;
10
import java.io.FileReader;
11
import java.io.IOException;
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15
16
/**
17
* @author Alpha
18
* JAVA 操作 excel 中的 .csv文件格式
19
*/
20
public class CsvUtil {
21
22
private String filename = null;
23
24
private BufferedReader bufferedreader = null;
25
26
private List list =new ArrayList();
27
28
public CsvUtil() {
29
30
}
31
32
public static void main(String[] args) throws IOException {
33
CsvUtil test = new CsvUtil();
34
test.run("D:/alpha/abc.csv");
35
}
36
37
public CsvUtil(String filename) throws IOException{
38
this.filename = filename;
39
bufferedreader = new BufferedReader(new FileReader(filename));
40
String stemp;
41
while((stemp = bufferedreader.readLine()) != null){
42
list.add(stemp);
43
}
44
}
45
46
public List getList() throws IOException {
47
return list;
48
}
49
50
public int getRowNum(){
51
return list.size();
52
}
53
54
public int getColNum(){
55
if(!list.toString().equals("[]")) {
56
if(list.get(0).toString().contains(",")) {
57
return list.get(0).toString().split(",").length;
58
}else if(list.get(0).toString().trim().length() != 0) {
59
return 1;
60
}else{
61
return 0;
62
}
63
}else{
64
return 0;
65
}
66
}
67
68
public String getRow(int index) {
69
if (this.list.size() != 0)
70
return (String) list.get(index);
71
else
72
return null;
73
}
74
75
public String getCol(int index){
76
if (this.getColNum() == 0){
77
return null;
78
}
79
StringBuffer scol = new StringBuffer();
80
String temp = null;
81
int colnum = this.getColNum();
82
if (colnum > 1){
83
for (Iterator it = list.iterator(); it.hasNext();) {
84
temp = it.next().toString();
85
scol = scol.append(temp.split(",")[index] + ",");
86
}
87
}else{
88
for (Iterator it = list.iterator(); it.hasNext();) {
89
temp = it.next().toString();
90
scol = scol.append(temp + ",");
91
}
92
}
93
String str=new String(scol.toString());
94
str = str.substring(0, str.length() - 1);
95
return str;
96
}
97
98
public String getString(int row, int col) {
99
String temp = null;
100
int colnum = this.getColNum();
101
if(colnum > 1){
102
temp = list.get(row).toString().split(",")[col];
103
}else if(colnum == 1) {
104
temp = list.get(row).toString();
105
}else{
106
temp = null;
107
}
108
return temp;
109
}
110
111
public void CsvClose() throws IOException {
112
this.bufferedreader.close();
113
}
114
115
public void run(String filename) throws IOException {
116
CsvUtil cu = new CsvUtil(filename);
117
/* List tt = cu.getList();
118
for (Iterator itt = tt.iterator(); itt.hasNext();){
119
System.out.println("==="+itt.next().toString());
120
}*/
121
for(int i=0;i<cu.getRowNum();i++){
122
123
String name = cu.getString(i,0);//得到第i行.第一列的數(shù)據(jù).
124
String email = cu.getString(i,1);;//得到第i行.第二列的數(shù)據(jù).
125
String tel = cu.getString(i,2);;
126
String number = cu.getString(i,3);;
127
128
System.out.println("===name:"+name);
129
System.out.println("===email:"+email);
130
System.out.println("===tel:"+tel);
131
System.out.println("===number:"+number);
132
System.out.println(" ");
133
}
134
135
/*System.out.println("aaa:"+cu.getRowNum());
136
System.out.println("bbb:"+cu.getColNum());
137
System.out.println("ccc:"+cu.getRow(0));
138
System.out.println("ddd:"+cu.getCol(0));
139
System.out.println("eee:"+cu.getString(0, 0));*/
140
141
cu.CsvClose();
142
}
143
144
}
145

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

二、java 對(duì).xls 文件格式的操作
1
2
package com.job5156.xlstodb;
3
4
import java.io.FileInputStream;
5
import java.io.InputStream;
6
7
import jxl.Cell;
8
import jxl.Workbook;
9
10
/**
11
* @author Alpha
12
* JAVA 操作 excel 中的 .xls文件格式
13
*/
14
public class ExcelUtil
15
{
16
public static void main(String[] args)
17
{
18
ExcelUtil eu = new ExcelUtil();
19
eu.run("D:/alpha/ab.xls");
20
}
21
22
private void run(String filename)
23
{
24
try
25
{
26
InputStream is = new FileInputStream(filename);
27
jxl.Workbook rwb = Workbook.getWorkbook(is);
28
//獲得總 Sheets
29
//Sheet[] sheets = rwb.getSheets();
30
//int sheetLen = sheets.length;
31
//獲得單個(gè)Sheets 含有的行數(shù)
32
jxl.Sheet rs = rwb.getSheet(0); //讀取第一個(gè)工作表的數(shù)據(jù)
33
//Cell[] cell_domain = rs.getColumn(0);//讀取第一列的值
34
int num = rs.getRows();//得到此excel有多少行..
35
for(int i=0;i<num;i++)
36
{
37
Cell[] cell = rs.getRow(i);//得到第i行的數(shù)據(jù)..返回cell數(shù)組
38
String name = cell[0].getContents();//得到第i行.第一列的數(shù)據(jù).
39
String email = cell[1].getContents();//得到第i行.第二列的數(shù)據(jù).
40
String tel = cell[2].getContents();
41
String number = cell[3].getContents();
42
43
System.out.println("===name:"+name);
44
System.out.println("===email:"+email);
45
System.out.println("===tel:"+tel);
46
System.out.println("===number:"+number);
47
System.out.println(" ");
48
}
49
}
50
catch(Exception ex)
51
{
52
ex.printStackTrace();
53
}
54
finally{
55
}
56
}
57
}
58

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
