package com.wellsoon.jyhd.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* @author Jimmy Liu
*/
public class ExcelReader {
private POIFSFileSystem excelFile = null;//file
private HSSFWorkbook wb = null;//book [includes sheet]
private HSSFSheet sheet = null;
private HSSFRow row = null;
private HSSFCell cell = null; //cell,the content which contains
private int sheetNum = 0; //第sheetnum个工作表
private int rowNum = 0;
private FileInputStream fis = null;
private String fileName = "";
public ExcelReader() {
}
public ExcelReader(String fileName) {
openFile(fileName);
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public void setSheetNum(int sheetNum) {
this.sheetNum = sheetNum;
}
/**
* 读取excel文件获得HSSFWorkbook对象
* @param fileName
*/
public void openFile(String fileName) {
this.fileName = fileName;
File f = new File(fileName);
if (f.exists()) {
try {
fis = new FileInputStream(f);
excelFile = new POIFSFileSystem(fis);
wb = new HSSFWorkbook(excelFile);
fis.close();
if(null == wb){
}else{
System.out.println("WorkBook不为空");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("===========================>>>>>>>>>>>>>>>>>>>>>>>>>excel File not exist");
}
}
/**
* 返回sheet表数目
* @return int
*/
public int getSheetCount() {
int sheetCount = -1;
sheetCount = wb.getNumberOfSheets();
return sheetCount;
}
/**
* sheetNum下的记录行数
* @return int
*/
public int getRowCount() {
if(null == wb) System.out.println("=============>WorkBook为空");
HSSFSheet sheet = wb.getSheetAt(this.sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 读取指定sheetNum的rowCount
* @param sheetNum
* @return int
*/
public int getRowCount(int sheetNum) {
HSSFSheet sheet = wb.getSheetAt(sheetNum);
int rowCount = -1;
rowCount = sheet.getLastRowNum();
return rowCount;
}
/**
* 得到指定行的内容
* @param lineNum
* @return String[]
*/
public String[] readExcelLine(int lineNum) {
return readExcelLine(this.sheetNum, lineNum);
}
/**
* 指定工作表和行数的内容
* @param sheetNum
* @param lineNum
* @return String[]
*/
public String [] readExcelLine(int sheetNum, int lineNum) {
if (sheetNum < 0 || lineNum < 0) return null;
String[] strExcelLine = null;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(lineNum);
int cellCount = row.getLastCellNum();
strExcelLine = new String[cellCount];
for (int i = 0; i < cellCount; i++) {
strExcelLine[i] = "";
if (null != row.getCell((short) i)) {
switch (row.getCell((short) i).getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA :
strExcelLine[i] = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC :
strExcelLine[i] = String.valueOf(row.getCell((short) i).getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING :
strExcelLine[i] = row.getCell((short) i).getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK :
strExcelLine[i] = "";
break;
default :
strExcelLine[i] = "";
break;
}
//如果读取的是科学计数法的格式,则转换为普通格式
//added by wangtongjun at 20060626
if (null != strExcelLine[i] &&
strExcelLine[i].indexOf(".") != -1 &&
strExcelLine[i].indexOf("E") != -1) {
DecimalFormat df = new DecimalFormat();
strExcelLine[i] = df.parse(strExcelLine[i]).toString();
}
//如果读取的是数字格式,并且以".0"结尾格式,则转换为普通格式
//added by wangtongjun at 20060713
if (null != strExcelLine &&
strExcelLine[i].endsWith(".0")) {
int size = strExcelLine[i].length();
strExcelLine[i] = strExcelLine[i].substring(0, size - 2);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strExcelLine;
}
/**
* 读取指定列的内容
* @param cellNum
* @return String
*/
public String readStringExcelCell(int cellNum) {
return readStringExcelCell(this.rowNum, cellNum);
}
/**
* 指定行和列编号的内容
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int rowNum, int cellNum) {
return readStringExcelCell(this.sheetNum, rowNum, cellNum);
}
/**
* 指定工作表、行、列下的内容
* @param sheetNum
* @param rowNum
* @param cellNum
* @return String
*/
public String readStringExcelCell(int sheetNum, int rowNum, int cellNum) {
String strExcelCell = "";
if (sheetNum < 0 || rowNum < 0) return null;
try {
sheet = wb.getSheetAt(sheetNum);
row = sheet.getRow(rowNum);
/**
* modified by wangtongjun
* bug description:
* if the cell is empty ,the behind data can't get
* for null point exception deal
*/
if (null != row.getCell((short) cellNum)) { //add this condition judge
switch (row.getCell((short) cellNum).getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA :
strExcelCell = "FORMULA ";
break;
case HSSFCell.CELL_TYPE_NUMERIC : {
strExcelCell = String.valueOf(row.getCell((short) cellNum).getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_STRING :
strExcelCell = row.getCell((short) cellNum).getStringCellValue();
break;
default :
}
//如果读取的是科学计数法的格式,则转换为普通格式
//added by wangtongjun at 20060626
if (null != strExcelCell &&
strExcelCell.indexOf(".") != -1 &&
strExcelCell.indexOf("E") != -1) {
DecimalFormat df = new DecimalFormat();
strExcelCell = df.parse(strExcelCell).toString();
}
//如果读取的�
没有合适的资源?快使用搜索试试~ 我知道了~
readExcel在jsp中读取excel文件
共22个文件
jar:5个
java:4个
class:4个
3星 · 超过75%的资源 需积分: 32 71 下载量 90 浏览量
2011-04-25
09:48:40
上传
评论
收藏 1.1MB RAR 举报
温馨提示
readExcel在jsp中读取excel文件;本人已经试过了,不错。还可以大家试试OK
资源推荐
资源详情
资源评论
收起资源包目录
readExcel.rar (22个子文件)
css
default.css 10KB
style.css 532B
.project 385B
WEB-INF
web.xml 270B
src
net
skycity
jyhd
log4j.properties 490B
com
wellsoon
jyhd
db
DBConnectionManager.java 2KB
service
impl
OnlineTestServiceImpl.java 7KB
util
WellsoonUtil.java 3KB
ExcelReader.java 11KB
lib
filemover.jar 2KB
log4j-1.2.11.jar 342KB
cos.jar 55KB
poi-2.5.1-final-20060810.jar 783KB
uploadbean.jar 30KB
classes
net
skycity
jyhd
log4j.properties 490B
com
wellsoon
jyhd
db
DBConnectionManager.class 3KB
service
impl
OnlineTestServiceImpl.class 3KB
util
ExcelReader.class 7KB
WellsoonUtil.class 3KB
index.jsp 4KB
upload.jsp 2KB
.classpath 574B
共 22 条
- 1
祈祷
- 粉丝: 26
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页