JAVA excelファイルの読み込み

2424 ワード

1.jxlをダウンロードする.jarパッケージ.classpathに参加します
2.プログラムコードは以下の通り
 
package com.wansha.io;

import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Sheet;
import jxl.Workbook;

public class ExcelReader {
	    
	    public static void main(String[] args){
	        readExcel("D:/123.xls"); 
	    }
	    
	    public static void readExcel(String filePath){

	    	try{
	                InputStream is=new FileInputStream(filePath);
	                 
	                //===    excel        
	                Workbook rwb = Workbook.getWorkbook(is);
	                 
	                //            :rwb.getSheet("abc")
	                Sheet st = rwb.getSheet(0);
	                
	                //     cell    ,getCell(int column, int row)    
	                int Rows=st.getRows();//   
	                int Cols=st.getColumns();//     
	            
	                /**
	                 * 
	                 */   
	                
	                for(int i=0;i<Rows;i++)
	                { 
	                    for(int j=0;j<Cols;++j)
	                    { 
	                        /**
	                         * getCell(int a,int b);
	                         * a:   
	                         * b:    
	                         *   :getCell(0,1);0:   .1:   ;
	                         * getCell      cell。     excel
	                         *       ,    :
	                         */        
	                        /** 
	                        Cell c = st.getCell(j,i);
	                        
	                        if(c.getType()==CellType.LABEL){
	                            LabelCell l = (LabelCell)c; 
	                            System.out.println(l.getString()); 
	                        }  
	                        else if(c.getType()==CellType.DATE){
	                            DateCell d =(DateCell)c;
	                            System.out.println(d.getDate()); 
	                        }*/
	                         
	                         
	                        System.out.print((st.getCell(j,i)).getContents());
	                    }
	                    System.out.print(" ");
	                } 
	                rwb.close(); 
	                is.close();    
	            }
	            catch(Exception e)
	            { 
	                e.printStackTrace();
	            }
       }
}