隨筆-86  評論-33  文章-0  trackbacks-0

          所需JAR: jxl.jar

          package  exceldemo.BO;

          import  java.io.InputStream;
          import  java.io.FileInputStream;
          import  java.io. * ;
          import  jxl.Workbook;
          import  jxl.read.biff.BiffException;
          import  jxl.Sheet;
          import  jxl.Cell;
          import  jxl.write.WritableWorkbook;
          import  jxl.write.WritableSheet;
          import  jxl.write.Label;
          import  jxl.write.WriteException;
          import  jxl.write.WritableCell;
          import  jxl.write.WritableImage;
          import  jxl.write.WritableFont;
          import  jxl.format.UnderlineStyle;
          import  jxl.write.WritableCellFormat;

          public   class  WriteExcel {
              
          public  WriteExcel() {
              }
              
              
          /**
               * 讀取Excel文件
               
          */
              
          public   void  readExcel(){
              
          try  {
                  InputStream is 
          =   new  FileInputStream( " c:\\ss.xls " );
                  Workbook workbook 
          =  Workbook.getWorkbook(is); // 構(gòu)建Workbook對象, 只讀Workbook對象
                  
                  System.out.println(
          " 工作表個數(shù): " + workbook.getNumberOfSheets());
                  Sheet[] sheet 
          =  workbook.getSheets(); // 獲取Sheet表
                   int  columns  =  sheet[ 0 ].getColumns();
                  
          int  rows  =  sheet[ 0 ].getRows();
                  System.out.println(
          " " + rows + " " + columns + " " );
                  
          for ( int  i = 0 ;i < rows;i ++ ){
                    
          for ( int  j = 0 ;j < columns;j ++ ){
                    Cell cell00 
          =  sheet[ 0 ].getCell(j,i);
                    System.out.print(cell00.getContents());
                    }
                    System.out.println(
          "" );
                  }
                  workbook.close();
                  is.close();
              } 
          catch  (FileNotFoundException ex) {ex.printStackTrace();
              } 
          catch  (BiffException ex) {ex.printStackTrace();
              } 
          catch  (IOException ex) {ex.printStackTrace();}

              }
              
              
          /**
               * 寫Excel文件
               
          */
              
          public   void  writeExcel(){
                 File newExcel 
          =   new  File( " c:\\newExcel.xls " );
              
          try  {
                  WritableWorkbook wwb 
          =  Workbook.createWorkbook(newExcel); // 創(chuàng)建可寫入的Excel工作薄
                  
          // Method 2:將WritableWorkbook直接寫入到輸出流
                   /*
                  OutputStream os = new FileOutputStream(targetfile);
                  jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
                  
          */

                  WritableSheet ws 
          =  wwb.createSheet( " TheFirstSheet " , 0 ); // 創(chuàng)建Excel工作表
                  
          // 普通文本
                  Label label00  =   new  Label( 0 , 0 , " derek " );
                  ws.addCell(label00);
                  WritableCell wc 
          =   new  Label( 1 , 0 , " man " );
                  ws.addCell(wc);
                  
          // 圖片
                  WritableImage wi  =   new  WritableImage( 1 , 5 , 1 , 3 , new  File( " c:\\300.PNG " )); // 參數(shù)表示(列位置,行位置,寬占列數(shù),高占行數(shù),圖片路徑)   
                  ws.addImage(wi);
                  
          // 格式化
                  WritableFont wfc  =   new  WritableFont(WritableFont.ARIAL, 10 ,WritableFont.BOLD, false ,UnderlineStyle.DOUBLE,jxl.format.Colour.RED);
                  WritableCellFormat wcfFC 
          =   new  jxl.write.WritableCellFormat(wfc);
                  Label labelCFC 
          =   new  jxl.write.Label( 2 0 " This is a Label Cell " , wcfFC);
                  ws.addCell(labelCFC);

                  
          // 寫入Exel工作表
                  wwb.write();
                  
          // 關(guān)閉Excel工作薄對象
                  wwb.close();

              } 
          catch  (IOException ex) {ex.printStackTrace();} 
                
          catch  (WriteException ex) {
                      
          /**  @todo Handle this exception  */
                  }

              }
              
              
          public   static   void  main(String[] args) {
                  WriteExcel writeexcel 
          =   new  WriteExcel();
                  writeexcel.writeExcel();
              }
          }
          posted on 2006-05-10 11:18 Derek.Guo 閱讀(327) 評論(0)  編輯  收藏 所屬分類: Java
          MSN:envoydada@hotmail.com QQ:34935442
          主站蜘蛛池模板: 中阳县| 泽普县| 江陵县| 八宿县| 成武县| 商丘市| 阳曲县| 嘉荫县| 额济纳旗| 建平县| 无锡市| 旅游| 荃湾区| 东安县| 宜城市| 吐鲁番市| 屯昌县| 北安市| 台中市| 象山县| 陵水| 安吉县| 锦屏县| 通道| 扬州市| 东安县| 台东市| 定兴县| 宝应县| 霍山县| 宜都市| 湛江市| 钟山县| 玉树县| 渭源县| 岐山县| 拉萨市| 昌邑市| 昆山市| 柳林县| 当雄县|