package test;
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 test {
/**
* @param args
*/
public static void main(String[] args) {
read("f://air1.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(0); //获取某一列的所有单元格,返回的是单元格对象数组
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());
}
/* for(int i = 0;i < cellCol.length;i++){
System.out.println("第一列字段:"+cellCol[i].getContents());
}
for(int i = 0;i < cellRow.length;i++){
System.out.println("第一行字段:"+cellRow[i].getContents());
}
System.out.println("第一个单元格内容:"+cell.getContents());*/
Cell[][] cells = new Cell[rsColumns][rsRows];
for(int j = 0;j < rsRows;j++){
for(int i = 0;i < rsColumns;i++){
Cell cell = rs.getCell(i, j);
System.out.print(cell.getContents()+" ");
}
System.out.println();
}
return cells; //以二维数组形式返回Excel的保存的所有数据
}
}
获取本地Excel文件内容
最新推荐文章于 2023-06-20 22:01:49 发布