jxl 读 excel

jxl 读 excel

      与写对应的是读.

 

package  net.blogjava.chenlb;

import  java.io.IOException;
import  java.io.InputStream;
import  java.util.ArrayList;
import  java.util.List;

import  jxl.Cell;
import  jxl.Sheet;
import  jxl.Workbook;
import  jxl.read.biff.BiffException;


/**
 * jxl 的Excel阅读器.
 * 
@author  chenlb 2007-10-20 下午01:36:01
 
*/
public   class  JxlExcelReader {
    
    
/**
     * 
@return  返回String[] 的列表
     
*/
    
public  List readExcel(InputStream in) {
        List lt 
=   new  ArrayList();
        Workbook wb 
=   null ;
        
        
try  {
            wb 
=  Workbook.getWorkbook(in);
            Sheet[] sheets 
=  wb.getSheets();     // 获取工作
             for ( int  i = 0 ; i < sheets.length; i ++ ) {
                Sheet sheet 
=  sheets[i];
                
for ( int  j = 0 ; j < sheet.getRows(); j ++ ) {
                    Cell[] cells 
=  sheet.getRow(j);     // 读取一行
                     if (cells  !=   null   &&  cells.length  >   0 ) {     // 这一行有内容才添加
                        String[] dataCells  =   new  String[cells.length];
                        
for ( int  k = 0 ; k < cells.length; k ++ ) {
                            dataCells[k] 
=   "" + cells[k].getContents();  // 读内容
                        } // column
                        lt.add(dataCells);
                    }
                }
// one sheet
            } // xls file
        }  catch  (BiffException e) {
            e.printStackTrace();
        } 
catch  (IOException e) {    
            e.printStackTrace();
        } 
finally  {
            
if (wb  !=   null ) {
                wb.close();
            }
        }
        
        
return  lt;
    }

}


你可能感兴趣的:(jxl 读 excel)