Java琐事

-I am not alone
posts - 54, comments - 30, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java处理excel

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

评论

# re: java处理excel  回复  更多评论   

2010-10-26 09:03 by heqs
POI也是不错的

# re: java处理excel[未登录]  回复  更多评论   

2010-10-26 11:08 by pop
excel2007,2010无法处理

# re: java处理excel  回复  更多评论   

2010-10-26 13:20 by syg
@pop
jxl的确不能处理excel2007和2010
但poi可以处理excel2007。所以你可以:
1. 转成2003格式,再处理
2. 使用poi吧

只有注册用户登录后才能发表评论。


网站导航: