jxl.Workbook 读取excel

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class WorkbookExcel {
 
 //文件路径
 private static String pathFile = "E://副本00非集中管理的关联客户.xls";
 
 /**
  * 读取excel文件
  * @return 返回以表格格式的二维数组
  */
 public String [][] readExcel(){
  try {
   File pathFlie = new File(pathFile);
   InputStream pathInput = new FileInputStream(pathFlie);
   Workbook workbook = Workbook.getWorkbook(pathInput);
   //第几个sheet
   Sheet sheet = workbook.getSheet(0);
   //总行数
   int sheetRows = sheet.getRows();
   //总列数
   int rsColumns = sheet.getColumns();
   return readTable( sheetRows, rsColumns,sheet);
  } catch (BiffException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 读取excel表格中的数据
  * @param sheetRows sheet中总行数
  * @param rsColumns sheet中中列数
  * @param sheet sheet对象
  * @return 返回以表格格式的二维数组
  */
 public static String[][] readTable(int sheetRows, int rsColumns, Sheet sheet){
  String [][] tableArray = new String[sheetRows][rsColumns];
  for(int i=0; i<sheetRows; i++){
   Cell[] cellRow = sheet.getRow(i);
   for(int j=0; j<cellRow.length;j++){
    String tableValue = cellRow[j].getContents().trim();
    tableArray[i][j] = tableValue;
   }
  }
  return tableArray;
 }
 }

posted @ 2012-08-15 16:58  萨赫鲁  阅读(561)  评论(0编辑  收藏  举报