码迷,mamicode.com
首页 > 编程语言 > 详细

关于java对Excel的读取

时间:2016-12-05 09:41:46      阅读:180      评论:0      收藏:0      [点我收藏+]

标签: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;
}
}

关于java对Excel的读取

标签:etc   exce   return   import   length   ++   for   new   tco   

原文地址:http://www.cnblogs.com/magic-melody/p/6132675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!