java编程之POI读取excel表格的内容

07版本的excel需要另外加一个jar包。xbean.jar的jar包

读取代码模板。利用模板介绍读取excel的一些poi的api这是重点

 /**
* 读取excel文件
* @Title: readExcel
* @Description: TODO(这里用一句话描述这个方法的作用)
* @author 尚晓飞
* @date 2014-11-10 上午8:58:01
* @param readPath 读取电脑硬盘上某个excel的绝对路径 例如:C://20141110中石油.xlsx
* @see com.bjsxt.sxf.service.ReadExcelService#readExcel(java.lang.String)]
* CELL_TYPE_NUMERIC 数值型 0
CELL_TYPE_STRING 字符串型 1
CELL_TYPE_FORMULA 公式型 2
CELL_TYPE_BLANK 空值 3
CELL_TYPE_BOOLEAN 布尔型 4
CELL_TYPE_ERROR 错误 5
*/
@Override
public void readExcel(String readPath) {
try {
//生成文件的输入流
InputStream inexcel=new FileInputStream(readPath);
//生成输入excel文件的内存模型
Workbook wb=WorkbookFactory.create(inexcel);
//获取具体表格名的对象
Sheet sheet=wb.getSheet("尚晓飞");
//Sheet sheet2=wb.getSheetAt(0);获取指定下标的表格对象。行和列的下标都是从0开始 //定义记录一行数据的值
Date date=null;//时间
double jiage=0;//价格
Integer xianliang=0;//现量
String borS=null;//类型 //获取excel表中存在值的行对象的迭代器
Iterator<Row> iterator=sheet.iterator();
while (iterator.hasNext()) {
Row row=iterator.next();
//获取excel表中存在值的某行的列对象的迭代器
Iterator<Cell> cIterator=row.cellIterator();
while (cIterator.hasNext()) {
Cell cell=cIterator.next();
if(cell.getCellType()==cell.CELL_TYPE_BLANK){
//如果单元格为空值,暂停本次循环继续下次循环
continue;
}
//获取当前单元格的列索引 。从0开始
Integer columnIndex=cell.getColumnIndex();
//获取当前单元格的行索引。从0开始
Integer rowIndex=cell.getRowIndex(); if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){//判断单元格的值是数字格式 if(HSSFDateUtil.isCellDateFormatted(cell)){//判断单元格是日期格式
SimpleDateFormat dateformat = new SimpleDateFormat("HH-mm");
//时间
date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());//获取成DATE类型
String fdate = dateformat.format(date);
System.out.println("rowIndex-->"+rowIndex+" columnIndex-->"+columnIndex+" value-->"+fdate); }else{ if(cell.getColumnIndex()==1){
//价格
jiage=cell.getNumericCellValue();
System.out.println("rowIndex-->"+rowIndex+" columnIndex-->"+columnIndex+" value-->"+jiage);
}else if(cell.getColumnIndex()==2){
//现量
xianliang=(int) cell.getNumericCellValue();
System.out.println("rowIndex-->"+rowIndex+" columnIndex-->"+columnIndex+" value-->"+xianliang);
}
} } if(cell.getCellType()==cell.CELL_TYPE_STRING){//单元格的值为字符串
//类型
borS=cell.getStringCellValue();
System.out.println("rowIndex-->"+rowIndex+" columnIndex-->"+columnIndex+" value-->"+borS);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
上一篇:核化主成分分析(Kernel PCA)应用及调参


下一篇:P3375 模板 KMP字符串匹配