001
JAVA 通常有两种方法来操作Excel,分别是POI和JExcelAPI,而且都是开源的。POI是Apace公司开发的,对中文的支持比较弱一些;而JExcelAPI是韩国公司开发的,不仅对中文的支持好,而且由于是纯JAVA编写的,所以可以跨平台操作。本文介绍的也是JExcelAPI的使用方法。
002
003
1
、环境配置
004
005
如下网址,可以下载到API:http:
006
007
下载完成的包解压之后,可以得到如下几个重要的文件:
008
009
(
1
)jxl.jar —— JExcelAPI 函数库;
010
011
(
2
)docs —— 帮助文档;
012
013
(
3
)src —— 源码文件夹;
014
015
将jxl.jar复制到%JAVA_HOME%\jre\ext\文件夹下面,在CLASSPATH变量里面添加
"%JAVA_HOME%\jre\ext"
,然后就可以调用JExcelAPI了。如果出现编译报错“找不到java.jxl包”,则可能是没有设置成功。这时,如果有Eclipse开发工具,可以在
"Build Path"
中添加
"External Library"
,找到jxl.jar的路径,然后就能编译成功了。
016
017
2
、Excel基础操作实例
018
019
(
1
) 创建Excel文件
020
021
/**读取Excel文件的内容
022
* @param file 待读取的文件
023
* @return // 生成Excel的类 */
024
package
createxls;
025
026
import
java.io.File;
027
028
import
jxl.Workbook;
029
import
jxl.write.Label;
030
import
jxl.write.WritableSheet;
031
import
jxl.write.WritableWorkbook;
032
033
public
class
CreateXLS {
034
public
static
void
main(String args[]) {
035
try
{
036
037
WritableWorkbook book = Workbook.createWorkbook(
new
File(
" test.xls "
));
038
039
WritableSheet sheet = book.createSheet(
" 第一页 "
,
0
);
040
041
042
Label label =
new
Label(
0
,
0
,
" test "
);
043
044
045
sheet.addCell(label);
046
047
048
book.write();
049
book.close();
050
051
}
catch
(Exception e) {
052
System.out.println(e);
053
}
054
}
055
}
056
057
058
059
(
2
)读Excel文件
060
061
062
package
readxls;
063
064
065
import
java.io.File;
066
067
import
jxl.Cell;
068
import
jxl.Sheet;
069
import
jxl.Workbook;
070
071
public
class
ReadXLS {
072
public
static
void
main(String args[]) {
073
try
{
074
Workbook book = Workbook.getWorkbook(
new
File(
" test.xls "
));
075
076
Sheet sheet = book.getSheet(
0
);
077
078
Cell cell1 = sheet.getCell(
0
,
0
);
079
String result = cell1.getContents();
080
System.out.println(result);
081
book.close();
082
}
catch
(Exception e) {
083
084
e.printStackTrace();
085
}
086
}
087
}
088
089
090
091
092
(
3
)合并单元格、格式化单元格等
093
094
095
096
097
package
additionalproperty;
098
099
import
java.io.*;
100
import
jxl.write.*;
101
import
jxl.*;
102
103
public
class
MergeCells {
104
public
static
void
main(String [] args){
105
try
{
106
WritableWorkbook book = Workbook.createWorkbook(
new
File(
"test.xls"
));
107
WritableSheet sheet = book.createSheet(
"第一页"
,
0
);
108
sheet.mergeCells(
3
,
3
,
6
,
6
);
109
110
111
WritableFont font =
new
WritableFont(WritableFont.TIMES,
30
, WritableFont.BOLD);
112
WritableCellFormat format =
new
WritableCellFormat(font);
113
format.setAlignment(jxl.format.Alignment.CENTRE);
114
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
115
format.setBackground(jxl.format.Colour.BLUE);
116
format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THICK);
117
118
Label label =
new
Label(
3
,
3
,
"合并"
, format);
119
sheet.addCell(label);
120
book.write();
121
book.close();
122
}
123
catch
(Exception e){
124
e.printStackTrace();
125
}
126
}
127
}
128
129
130
131
(
4
)添加图片
132
133
JExcelAPI对图片的操作有限:它不能生成图表、图片和宏,但是复制工作表时,这些信息可以保留复制。而且当向工作表中添加图片时,只能支持PNG格式的图片。
134
135
136
137
138
package
handleimage;
139
140
import
java.io.*;
141
import
jxl.*;
142
import
jxl.write.*;
143
144
public
class
CreateImage {
145
public
static
void
main(String [] args){
146
try
{
147
WritableWorkbook book = Workbook.createWorkbook(
new
File(
"test.xls"
));
148
WritableSheet sheet = book.createSheet(
"第一页"
,
0
);
149
WritableImage image =
new
WritableImage(
3.5
,
3.5
,
4.3
,
8.7
,
150
new
File(
"C:\\Documents and Settings\\Wei Li\\My Documents\\My Pictures\\Water lilies.PNG"
));
151
sheet.addImage(image);
152
153
book.write();
154
book.close();
155
}
156
catch
(Exception e){ e.printStackTrace(); }
157
}
158
}