隨筆-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); // 構建Workbook對象, 只讀Workbook對象
                  
                  System.out.println(
          " 工作表個數: " + 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); // 創建可寫入的Excel工作薄
                  
          // Method 2:將WritableWorkbook直接寫入到輸出流
                   /*
                  OutputStream os = new FileOutputStream(targetfile);
                  jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
                  
          */

                  WritableSheet ws 
          =  wwb.createSheet( " TheFirstSheet " , 0 ); // 創建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 " )); // 參數表示(列位置,行位置,寬占列數,高占行數,圖片路徑)   
                  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();
                  
          // 關閉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
          主站蜘蛛池模板: 慈利县| 诸暨市| 景东| 开封市| 卢龙县| 奎屯市| 称多县| 高碑店市| 都江堰市| 房产| 三明市| 信阳市| 揭东县| 麟游县| 兴安盟| 英吉沙县| 岗巴县| 聊城市| 金山区| 阳城县| 丰宁| 武冈市| 饶河县| 滦平县| 溆浦县| 滁州市| 二手房| 漳浦县| 大姚县| 嘉义县| 丹江口市| 北票市| 长顺县| 深泽县| 松溪县| 抚远县| 灌云县| 嵊泗县| 商河县| 漳浦县| 故城县|