标签:etc exce return import length ++ for new tco
/*
注意:读取的Excel文件 请另存为2003版本的Excel,否则可能会报错
别忘记导入第三方的jar包
*/
package com.zzp.ExcelParse;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.IOException;
/**
 * 对Excel表的读取
 * Created by hasee on 2016/11/29.
 */
public class ObtainExcelData {
    private Workbook workbook = null;
    private Sheet sheet = null;
    /*
     @see:加载文件
     @parma:path 文件路径
     @return:执行是否成功
     */
    public boolean getFile(String path) {
        if (path.length() == 0 || path == null) {
            return false;
        }
        try {
            workbook = Workbook.getWorkbook(new File(path));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return false;
    }
    /*
     @see:根据表的下标得到表
     @parma:sheetIndex 标的下标
     @return:执行是否成功
     */
    public boolean getSheet(int sheetIndex) {
        if (sheetIndex >= 0) {
            sheet = workbook.getSheet(sheetIndex);
            return true;
        }
        return false;
    }
    /*
     @see:根据表名得到表
     @parma:sheetName 表名
     @return:执行是否成功
     */
    public boolean getSheet(String sheetName) {
        if (sheetName.length() > 0 || sheetName != null) {
            sheet = workbook.getSheet(sheetName);
            return true;
        }
        return false;
    }
    /*
    @see:按列读取数据(不读取表头)
    @parma:rowIndex查询的列数
    @parma:row excel表的行数
    @parma:rowData 储存获取到的一列数据
    @return:获取到的一列数据
     */
    public String[] getCol(int colIndex) {
        if (colIndex < 0) {
            return null;
        }
        int row = sheet.getRows();
        String[] rowData = new String[row];
        Cell cell = null;
        for (int i = 1; i < row; i++) {
            cell = sheet.getCell(colIndex, i);
            String strRow = cell.getContents();
            rowData[i - 1] = strRow;
        }
        return rowData;
    }
    /*
    @see:按行读取数据
    @parma:rowIndex 查询的行数
    @parma:col Eccel表的列数
    @parma:rowData 储存获取到的一行数据
    @return:获取到的一行数据
    */
    public String[] getRow(int rowIndex) {
        if (rowIndex < 0) return null;
        int col = sheet.getColumns();
        String[] rowData = new String[col];
        Cell cell = null;
        for (int i = 0; i < col; i++) {
            cell = sheet.getCell(i, rowIndex);
            String strRow = cell.getContents();
            rowData[i] = strRow;
        }
        return rowData;
    }
}
标签:etc exce return import length ++ for new tco
原文地址:http://www.cnblogs.com/magic-melody/p/6132675.html