您的位置:首页 > 编程语言 > Java开发

Java怎样从Excel文件中读取数据

2007-05-17 16:30 816 查看
用Java怎样从Excel文件中读取数据
package cn.com.spaceware.lixun;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

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 cn.com.spaceware.sxd.SC_BANK_DATABASE.DATABEAN.RepairInfoBean;

/**
* 从Excel文件中读取ATM机维护信息
*
* Author: Shune LEE
*
* Date: November 22nd,2005
*/

public class ReadRepairInfo{

private boolean isCancel=false; //控制读取是否继续进行的参数
private String filePath=null; //Excel文件的存放全路径

/*控制格式的变量*/
private int headNum=2; //控制从第headNum行开始读取,因为行的索引是从0开始的
private int leftNum=0; //控制从第leftNum列开始读取,因为列的索引是从0开始的

ReadRepairInfo(String file){
this.filePath=file;
}

/**
*
* @author Shune Lee
*
* TODO 从Excel文件中读取维护信息,并添加到RepairInfoBean中去
*
*/
public ArrayList readExcel4RepairInfo(){
ArrayList table=new ArrayList();
ArrayList record=new ArrayList();

try {

HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(filePath));
HSSFSheet sheet = workbook.getSheetAt(0); //得到一个Sheet

/*获得导入的记录数,也就是数据的行数*/
int rowNum=(sheet.getLastRowNum()-sheet.getFirstRowNum()+1)-headNum; //行的索引是从0开始的

/*遍历所有数据,并将得到的数据传给rInfo*/
for(int i=headNum;i
/*如果用户将isCancel设为true,则停止读取*/
if(isCancel)
break;

HSSFRow row = sheet.getRow(i);

for(short j=row.getFirstCellNum();j
String cellContent1=null;
Double cellContent2=null;
HSSFCell cell=row.getCell(j);

/*判断单元格内容的类型,并赋给相应的变量*/
if(cell.getCellType()==1){
cellContent1=cell.getStringCellValue();
record.add(cellContent1);
}
else if(cell.getCellType()==0){
cellContent2=new Double(cell.getNumericCellValue());
record.add(cellContent2);
}
else
System.out.println("Excel中的数据有问题,请检查。");
}
table.add(getRepairInfoBean(record));
}

} catch (FileNotFoundException e) {
System.out.println("找不到文件 " + filePath);
e.printStackTrace();
} catch (IOException e) {
System.out.println("访问文件出错 " + filePath);
e.printStackTrace();
}

return table;
}

/**
*
* @author Shune Lee
*
* TODO 用从Excel读取的记录生成RepairInfoBean
*
* @param ArrayList 从Excel读取的一条记录,即一行
*/
public RepairInfoBean getRepairInfoBean(ArrayList content){
RepairInfoBean rpInfo=new RepairInfoBean();

if(content!=null&&!(content.isEmpty())){

/*给RepairInfoBean的对象传入数据*/
rpInfo.setRepairID(((Double)content.get(0)).intValue());
rpInfo.setStrATMEndCode(((Double)content.get(1)).intValue());
rpInfo.setDEPLOYID(((Double)content.get(2)).intValue());
rpInfo.setStrRepairType(((Double)content.get(3)).intValue());
rpInfo.setStrContent((String)content.get(4));
rpInfo.setStrPersonName((String)content.get(5));
rpInfo.setStrDate(new String().valueOf(content.get(6)));
System.out.println("The size is="+content.size());
}

return rpInfo;
}

/*测试函数*/
public static void main(String[] args){
ReadRepairInfo readRepairInfo=new ReadRepairInfo("E://test.xls");

ArrayList list=readRepairInfo.readExcel4RepairInfo();

Iterator it=list.iterator();
while(it.hasNext()){
RepairInfoBean rpInfo=(RepairInfoBean)it.next();
System.out.println("RepairID="+rpInfo.getRepairID()+"; "+"strContent="+rpInfo.getStrContent());
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: