java读取excel 文件

 


java操作excel 

1.下载 jxl.jar包。并加入classpath里面去。

2.程序代码如下

 

package  com;

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


import  jxl.Sheet;
import  jxl.Workbook;
   
/** */ /**
 * 
 *
 * 
@author <a href="mailto:[email protected]">qinglong.lu</a>
 *
 * 2008-3-21
 
*/
 

public   class  OperateExcel  ... {
    
    
public static void main(String[] args)...{
        readExcel(
"D:\1.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();
            }

        }

             

}

 

 

你可能感兴趣的:(java,C++,c,Excel,C#)