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
|
package
读取excel;
/*
*导入jxl包,注意不能读取最新版本的Excel文件
*/
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.InputStream;
import
jxl.Cell;
import
jxl.CellType;
import
jxl.Sheet;
import
jxl.Workbook;
import
jxl.write.Label;
public
class
ExcelOperater {
public
static
void
main(String[] args) {
jxl.Workbook readwb =
null
;
try
{
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
InputStream instream =
new
FileInputStream(
"C:\\Users\\Administrator\\Desktop\\测试用例和结果1.xls"
);
readwb = Workbook.getWorkbook(instream);
//Sheet的下标是从0开始
//获取第一张Sheet表
Sheet readsheet = readwb.getSheet(
0
);
//获取Sheet表中所包含的总列数
int
rsColumns = readsheet.getColumns();
//获取Sheet表中所包含的总行数
int
rsRows = readsheet.getRows();
//获取指定单元格的对象引用
for
(
int
i =
0
; i < rsRows; i++) {
for
(
int
j =
0
; j < rsColumns; j++) {
Cell cell = readsheet.getCell(j, i);
System.out.print(cell.getContents() +
" "
);
}
System.out.println();
}
//利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(
new
File(
"C:\\Users\\Administrator\\Desktop\\测试用例和结果1.xlsx"
), readwb);
//读取第一张工作表
jxl.write.WritableSheet ws = wwb.getSheet(
0
);
//获得第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(
0
,
0
);
//判断单元格的类型, 做出相应的转化
if
(wc.getType() == CellType.LABEL) {
Label l = (Label) wc;
l.setString(
"新姓名"
);
}
//写入Excel对象
wwb.write();
wwb.close();
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
readwb.close();
}
}
}
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1876277