Posted on 2010-10-25 23:00
石子路口 阅读(2244)
评论(3) 编辑 收藏 所属分类:
网络教学资源平台
使用jxl.jar,让java可以处理excel表,下载jxl.jar的时候注意版本要和java虚拟机版本兼容
下面是类:
1
package excel;
2
3
import java.io.File;
4
5
import jxl.Cell;
6
import jxl.Sheet;
7
import jxl.Workbook;
8
import jxl.write.Label;
9
import jxl.write.WritableSheet;
10
import jxl.write.WritableWorkbook;
11
12
public class ExcelOperation
13

{
14
15
public static void main(String[] args) throws Exception
16
{
17
File file = new File("F:\\ceshi1.xls");
18
File file2 = new File("F:\\3883-3956.xls");
19
processExcel(file, file2, "年价明细");
20
}
21
22
public static void processExcel(File file, File file2, String sheetName) throws Exception
23
{
24
Workbook workbook = Workbook.getWorkbook(file);
25
Sheet sheet = workbook.getSheet(sheetName);
26
27
//创建新的excel
28
if(file2.exists())
29
{
30
file2.delete();
31
}
32
WritableWorkbook writableWorkbook = Workbook.createWorkbook(file2);
33
WritableSheet writableSheet = writableWorkbook.createSheet(sheetName, 0);
34
Label label;
35
36
int j = 6;
37
String last1 = "";
38
String last2 = "";
39
String last3 = "";
40
41
for(int i = 3882 ; i <= 3955 ; i++)
42
{
43
Cell cell = sheet.getCell(0,i);
44
45
String content = cell.getContents();
46
if(content.indexOf(",") < 0)
47
{
48
j++;
49
label = new Label(0,j," "+content);
50
writableSheet.addCell(label);
51
}
52
else
53
{
54
55
String[] str = content.split(",");
56
57
if(!str[0].equals(last1))
58
{
59
j++;
60
label = new Label(0,j," "+str[0]);
61
writableSheet.addCell(label);
62
last1 = str[0];
63
}
64
65
66
if(!(str[0]+","+str[1]).equals(last2))
67
{
68
j++;
69
label = new Label(0,j," "+str[1]);
70
writableSheet.addCell(label);
71
last2 = str[0]+","+str[1];
72
}
73
74
if(!(str[0]+","+str[1]+","+str[2]).equals(last3))
75
{
76
j++;
77
label = new Label(0,j," "+str[2]);
78
writableSheet.addCell(label);
79
last3 = str[0]+","+str[1]+","+str[2];
80
}
81
82
}
83
84
}
85
86
writableWorkbook.write();
87
writableWorkbook.close();
88
89
}
90
91
}
92