天天看点

java excel 内容 代码,java读取本地excel文件代码

package cn.com.view.read;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

public class ReadExcel {

public static void main(String[] args){

read("d://air.xls");

}

public static Cell[][] read(String fileName){

Workbook rwb = null;

InputStream is;

try {

is = new FileInputStream(fileName);

rwb = Workbook.getWorkbook(is);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (BiffException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

int sheets = rwb.getNumberOfSheets();

Sheet rs = rwb.getSheet(0);

String sheetName = rs.getName();//获取Sheet的名称

int rsColumns = rs.getColumns();//获取Sheet表中所包含的总列数

Cell[] cellCol = rs.getColumn(1);  //获取某一列的所有单元格,返回的是单元格对象数组

int rsRows = rs.getRows();  //获取Sheet表中所包含的总行数

Cell[] cellRow = rs.getRow(1); //获取某一行的所有单元格,返回的是单元格对象数组

//  Cell cell = rs.getCell(0, 0); //第一个是列数,第二个是行数

System.out.println("Sheet的个数:"+sheets);

System.out.println("Sheet的名称:"+sheetName);

System.out.println("列数:"+rsColumns);

System.out.println("行数:"+rsRows);

/* for(int i = 0;i < cellCol.length;i++){

System.out.println("第一列字段:"+cellCol[i].getContents());