Java瑣事

          -I am not alone
          posts - 54, comments - 30, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          java處理excel

          Posted on 2010-10-25 23:00 石子路口 閱讀(2252) 評論(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        //創(chuàng)建新的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吧
          主站蜘蛛池模板: 蒙城县| 新河县| 启东市| 武冈市| 宁德市| 靖边县| 和顺县| 无锡市| 铜梁县| 禄丰县| 谢通门县| 左云县| 隆化县| 高清| 鄯善县| 郧西县| 如皋市| 花莲县| 云龙县| 绥宁县| 陈巴尔虎旗| 临西县| 阳泉市| 晴隆县| 梨树县| 平乐县| 繁昌县| 汾阳市| 乐陵市| 西林县| 温泉县| 兴安县| 蒙自县| 达孜县| 昭苏县| 吴堡县| 阿瓦提县| 宁远县| 阳东县| 桐柏县| 商丘市|