随笔-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 阅读(316) 评论(0)  编辑  收藏 所属分类: Java

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


网站导航:
 
MSN:envoydada@hotmail.com QQ:34935442