java工具代码-读取excel数据

2019-02-28  本文已影响0人  王古
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import jxl.Sheet;  
import jxl.Workbook;  
import jxl.read.biff.BiffException;  


public class G_Excel {
    public static void main(String arg[]){
        G_Excel obj = new G_Excel();
        File file = new File("F:/***********.xls");  
        obj.readExcel(file); 
        
    }
    
    public void readExcel(File file){
        try{
            InputStream is = new FileInputStream(file.getAbsolutePath());  
            // jxl提供的Workbook类  
            Workbook wb = Workbook.getWorkbook(is);  
            // Excel的页签数量  
            int sheet_size = wb.getNumberOfSheets();  
            //System.out.println(sheet_size);
            for (int index = 0; index < sheet_size; index++) {  
                //每个页签创建一个Sheet对象  
                Sheet sheet = wb.getSheet(index);  
                // sheet.getRows()返回该页的总行数  
                for (int i = 0; i < sheet.getRows(); i++) {  
                    // sheet.getColumns()返回该页的总列数  
                    for (int j = 0; j < sheet.getColumns(); j++) {  
                        String cellinfo = sheet.getCell(j, i).getContents();  
                        System.out.println(cellinfo);  
                        }  
                    }  
                }  
            }
        catch (FileNotFoundException e) {  
            e.printStackTrace();  
            } catch (BiffException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        } 
    }
    

上一篇下一篇

猜你喜欢

热点阅读