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());
}
}
}
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());
}
}
}
相关文章推荐
- 使用JAVA读取EXCEL文件里面的数据
- java读取excel文件中的数据
- 用 java 读取excel 文件里的时间格式数据时,24时制会自动转化为12时制,解决办法
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- JXL 读取 Excel java中jxl导出数据到excel的例子 上传文件
- 《Java开发有一说一》——Java读取Excel文件中的数据
- Java读取Excel文件中的数据
- JXL 读取 Excel java中jxl导出数据到excel的例子 上传文件
- Java读取、写入、处理Excel文件中的数据
- java读取excel文件获取数据
- Java数据导入功能之读取Excel文件实例
- Java读取、写入、处理Excel文件中的数据
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- 将后台数据读取到前台的EXCEL文件中去,用javascript实现,asp.net,javacript(发一个原创)
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- Java中读取Excel文件的内容和导出数据到Excel文件
- 用 java 读取excel 文件里的时间格式数据时,24时制会自动转化为12时制,解决办法
- Java读取、写入、处理Excel文件中的数据
- java读取excel文件数据
- Java, 如何从Excel中读取数据到文件中