一起學java

           

          對Excel表格的讀寫操作

          由于經常要涉及到對Excel的操作,而且數據量都比較大,所以開發一些代碼來代替人工操作就顯得非常必要了.
          在晚上找了好久,發現支持對Excel操作的第三方jar包還是不少的,當時由于時間緊就選擇了個jexcelapi_2_6_6.tar.gz,后來在使用的過程中發現問題還是不少.
          1,對Excel表格好像對文本(就是單元格格式是文本)支持比較好,其它很容易出問題
          2,只支持Excel2007版本之前的版本,暫不支持2007,有待改進.
          3,數據量過大很容易造成內存溢出(有解決的辦法,稍候)
          package com.infothunder.drm.oma1.excel;

          import java.io.BufferedWriter;
          import java.io.File;
          import java.io.FileWriter;
          import java.io.IOException;

          import jxl.Sheet;
          import jxl.Workbook;
          import jxl.read.biff.BiffException;
          import jxl.write.Label;
          import jxl.write.WritableSheet;
          import jxl.write.WritableWorkbook;
          import jxl.write.WriteException;
          import jxl.write.biff.RowsExceededException;

          import com.infothunder.drm.oma1.makeDcf.biz.ToPinyin;
          import com.infothunder.drm.util.ByteArrayUtil;

          public class ExcelToSongExcel{

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args){
                  String srcFile 
          = args[0];
                  String decFile 
          = args[1];
                  String sqlFile 
          = args[2];
                  toSongExcelFile(srcFile, decFile, sqlFile);
              }

              
              
          public static void toSongExcelFile(String srcFile, String decFile, String sqlFile){
                  
                  String f1 
          = srcFile;    
                  String f2 = decFile;    
                  String f3 = sqlFile;    
                  try{
                      Workbook workbook 
          = Workbook.getWorkbook(new File(f1));
                      WritableWorkbook hw 
          = Workbook.createWorkbook(new File(f2));
                      Sheet[] sheets 
          = workbook.getSheets();
                      
          //StringBuffer sb = new StringBuffer();
                      FileWriter fw = new FileWriter(f3, true);
                      BufferedWriter bw 
          = new BufferedWriter(fw);
                      System.out.println(
          "sheet工作簿的數量是:"+sheets.length);
                      
          for(int i=0;i<sheets.length;i++){
                          Sheet sheet 
          = sheets[i];
                          String she 
          = "sheet"+i;
                          WritableSheet ws 
          = hw.createSheet(she, i);
                          
          //int columns = sheet.getColumns();   //檢查表的列數
                          int rows = sheet.getRows();           //行數
                          System.out.println(rows);
                          
                          
          for(int j=1;j<rows;j++){
                              String id 
          = sheet.getCell(14, j).getContents();                   
                              ChangString.checkId(id);
                              Label l0 
          = new Label(0,j-1,id);                                      //0
                              ws.addCell(l0);
                              Label l1 
          = new Label(1,j-1,id);                                      //1
                              ws.addCell(l1);

                              String musicName = sheet.getCell(2, j).getContents();
                              String fileName 
          = ToPinyin.getFormattedLetterTitle(musicName);     
                              
                              Label l4 = new Label(4,j-1,fileName);                                 //4
                              ws.addCell(l4);
                              String filename 
          = sheet.getCell(1, j).getContents();               
                              Label l5 = new Label(5,j-1,filename);                                 //5
                              ws.addCell(l5);
                              String artist 
          = sheet.getCell(4, j).getContents();                  
                              String art = ToPinyin.getFormattedLetterTitle(artist);              
                              Label l6 = new Label(6,j-1,art);                                      //6
                              ws.addCell(l6);
                              Label l7 
          = new Label(7,j-1,artist);                                   //7
                              ws.addCell(l7);
                              String singerType 
          = sheet.getCell(5, j).getContents();             
                              String st = ChangString.changSingerType(singerType);
                              
          //NumberCell l8 = new NumberCell(8,j,st);
                              Label l8 = new Label(8,j-1,st);                                       //8
                              ws.addCell(l8);
                              String district 
          = sheet.getCell(6, j).getContents();
                              String dist 
          = ChangString.changDistrict(district);
                              Label l9 
          = new Label(9,j-1,dist);
                              ws.addCell(l9);
                              String genre 
          = sheet.getCell(9, j).getContents();                  
                              String style = ChangString.changGenre(genre);
                              Label l12 
          = new Label(12,j-1,style);                                  //12
                              ws.addCell(l12);
                              String time 
          = sheet.getCell(12, j).getContents();                  
                              int length = Integer.parseInt(time);
                              Label l14 
          = new Label(14,j-1,time);                                   //14
                              ws.addCell(l14);
                              String language 
          = sheet.getCell(11, j).getContents();               
                              String lang = ChangString.changLanguage(language);
                              Label l15 
          = new Label(15,j-1,lang);                                   //15
                              ws.addCell(l15);
                              
          byte[] b = String.valueOf(Math.random()).getBytes();
                              b 
          = ByteArrayUtil.MD5Jdk(b);
                              String ms 
          = ByteArrayUtil.byteArray2HexString(b);
                              Label l16 
          = new Label(16,j-1,ms);                                     
                              ws.addCell(l16);
                              String CPID 
          = sheet.getCell(20, j).getContents();                    
                              Label l17 = new Label(17,j-1,CPID);
                              ws.addCell(l17);                                                    
          //17 CPID
                              String contentId = sheet.getCell(21, j).getContents();
                              Label l18 
          = new Label(18,j-1,contentId);                                //18 ContenId
                              ws.addCell(l18);
                              String ringId 
          = sheet.getCell(22, j).getContents();
                              Label l19 
          = new Label(19,j-1,ringId);
                              ws.addCell(l19);
                              String price 
          = sheet.getCell(23,j).getContents();                    
                              Label l20 = new Label(20,j-1,price);
                              String amt 
          = "200";

                              price = ChangString.getPrice(price);
                              
          if(price=="0"||price==null||price.equals("")){
                                  amt 
          = "200";
                              }
          else{
                                  amt 
          = price;
                              }

                              System.out.println(price);
                              ws.addCell(l20);                                                    
          //
                              String startDate = sheet.getCell(24,j).getContents();                
                              startDate = ChangString.getSimpleDate(startDate);
                              String endDate 
          = sheet.getCell(25,j).getContents();                    
                              endDate = ChangString.getSimpleDate(endDate);
                              Label l21 
          = new Label(21,j-1,startDate);

                              ws.addCell(l21);
                            
                              Label l22 = new Label(22,j-1,endDate);
                              ws.addCell(l22);
                              String DL 
          = sheet.getCell(16,j).getContents();                        //以下都是渠道ID及其關聯
                              Label l23 = new Label(23,j-1,DL);
                              ws.addCell(l23);
                              String CH 
          = sheet.getCell(17,j).getContents();
                              Label l24 
          = new Label(24,j-1,CH);
                              ws.addCell(l24);
                              String CCH 
          = sheet.getCell(18,j).getContents();
                              Label l25 
          = new Label(25,j-1,CCH);
                              ws.addCell(l25);
                              String BID 
          = sheet.getCell(19,j).getContents();
                              Label l26 
          = new Label(26,j-1,BID);
                              ws.addCell(l26);
                              String publishDate 
          = sheet.getCell(10, j).getContents();            //publishDate
                              String pd = ChangString.changPublicDate(publishDate, BID);
                              Label l13 
          = new Label(13,j-1,pd);//13
                              ws.addCell(l13);
                              
                              String dir 
          = sheet.getCell(0, j).getContents();
                              dir 
          = ChangString.checkDir(dir);
                              Label l27 
          = new Label(27,j-1,dir);
                              ws.addCell(l27);
                              
                              String contentDescription 
          = (musicName + time).toString();
                              String mediaPath 
          = "resource2/dcf"+BID+"/"+CPID+"/";
                              String sql 
          = "insert into tsongs" 
                                  
          + "(id,contentName,tcp_id,singerName,genre,publishDate,district,mediaPath," 
                                  
          + "mediaFile,mediaType,aesKey,startDate,endDate,sid,regTime,modTime," 
                                  
          + "duration,language,state,keyword,contentId,ringId,amt,contentDescription)"
                                  
          + " values ('"
                                  
          + id + "','" + musicName + "','" + CPID + "','"    + artist + "',"    + style + ",'"
                                  
          + pd+ "'," + dist + ",'" + mediaPath + "','" + id + "',0," + "'" + ms + "','"
                                  
          + startDate + "','"    + endDate + "','" + id + "',now(),"    + "now()," + length + ","    + lang + ","
                                  
          + "1,'"    + fileName + "','"    + contentId + "','"    + ringId + "',"    + amt + ",'" + contentDescription + "');";
                              System.out.println(sql);
                              bw.write(sql);
                              bw.newLine();
                              bw.flush();
                              
                          }

                      }

                      hw.write();
                      hw.close();
                  }
          catch(BiffException ex){
                      ex.printStackTrace();
                  }
          catch(IOException ex){
                      ex.printStackTrace();
                  }
          catch(RowsExceededException ex){
                      ex.printStackTrace();
                  }
          catch(WriteException ex){
                      ex.printStackTrace();
                  }
          catch(Exception ex){
                      ex.printStackTrace();
                  }

              }


          }

          對于上面的代碼如果操作表格量太大的話可以在命令前加上
          java -Xmn128M -Xms512M -Xmx1024M -XX:PermSize=64M -XX:MaxPermSize=128m -classpath......
          意思上指定內存最大128M
          希望有支持Excel2007jar的朋友跟我說下,我好對代碼進行改造,怎樣更好支持非文本的單元格,謝謝大家的交流.

          posted on 2008-06-09 16:33 蟲子 閱讀(592) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 清水县| 营山县| 北辰区| 南昌县| 安新县| 舞阳县| 鄂尔多斯市| 格尔木市| 绵阳市| 门头沟区| 福建省| 毕节市| 个旧市| 久治县| 苏尼特左旗| 潜山县| 凤台县| 西乌珠穆沁旗| 泸水县| 卫辉市| 玉溪市| 湘乡市| 福建省| 宁河县| 镇康县| 镇江市| 嘉义市| 德庆县| 福州市| 晴隆县| 嘉祥县| 武清区| 郯城县| 温宿县| 铜鼓县| 札达县| 裕民县| 大连市| 苍南县| 武宁县| 济阳县|