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());
  }
  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的保存的所有数据
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值