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

java的poi技术读取和导入Excel

2012-02-22 00:36 525 查看
项目结构:
http://www.cnblogs.com/hongten/gallery/image/111987.html
用到的Excel文件:
http://www.cnblogs.com/hongten/gallery/image/111988.html

XlsMain.java类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)


importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;

importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
*
*@authorHongten</br>
*
*参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
publicclassXlsMain{

publicstaticvoidmain(String[]args)throwsIOException{
XlsMainxlsMain=newXlsMain();
XlsDtoxls=null;
List<XlsDto>list=xlsMain.readXls();

try{
XlsDto2Excel.xlsDto2Excel(list);
}catch(Exceptione){
e.printStackTrace();
}
for(inti=0;i<list.size();i++){
xls=(XlsDto)list.get(i);
System.out.println(xls.getXh()+""+xls.getXm()+""
+xls.getYxsmc()+""+xls.getKcm()+""
+xls.getCj());
}

}

/**
*读取xls文件内容
*
*@returnList<XlsDto>对象
*@throwsIOException
*输入/输出(i/o)异常
*/
privateList<XlsDto>readXls()throwsIOException{
InputStreamis=newFileInputStream("pldrxkxxmb.xls");
HSSFWorkbookhssfWorkbook=newHSSFWorkbook(is);
XlsDtoxlsDto=null;
List<XlsDto>list=newArrayList<XlsDto>();
//循环工作表Sheet
for(intnumSheet=0;numSheet<hssfWorkbook.getNumberOfSheets();numSheet++){
HSSFSheethssfSheet=hssfWorkbook.getSheetAt(numSheet);
if(hssfSheet==null){
continue;
}
//循环行Row
for(introwNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
HSSFRowhssfRow=hssfSheet.getRow(rowNum);
if(hssfRow==null){
continue;
}
xlsDto=newXlsDto();
//循环列Cell
//0学号1姓名2学院3课程名4成绩
//for(intcellNum=0;cellNum<=4;cellNum++){
HSSFCellxh=hssfRow.getCell(0);
if(xh==null){
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCellxm=hssfRow.getCell(1);
if(xm==null){
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCellyxsmc=hssfRow.getCell(2);
if(yxsmc==null){
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCellkcm=hssfRow.getCell(3);
if(kcm==null){
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCellcj=hssfRow.getCell(4);
if(cj==null){
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
returnlist;
}

/**
*得到Excel表中的值
*
*@paramhssfCell
*Excel中的每一个格子
*@returnExcel中每一个格子中的值
*/
@SuppressWarnings("static-access")
privateStringgetValue(HSSFCellhssfCell){
if(hssfCell.getCellType()==hssfCell.CELL_TYPE_BOOLEAN){
//返回布尔类型的值
returnString.valueOf(hssfCell.getBooleanCellValue());
}elseif(hssfCell.getCellType()==hssfCell.CELL_TYPE_NUMERIC){
//返回数值类型的值
returnString.valueOf(hssfCell.getNumericCellValue());
}else{
//返回字符串类型的值
returnString.valueOf(hssfCell.getStringCellValue());
}
}

}


XlsDto2Excel.java类

//该类主要负责向Excel(2003版)中插入数据

importjava.io.FileOutputStream;
importjava.io.OutputStream;
importjava.util.List;

importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRichTextString;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

publicclassXlsDto2Excel{

/**
*
*@paramxls
*XlsDto实体类的一个对象
*@throwsException
*在导入Excel的过程中抛出异常
*/
publicstaticvoidxlsDto2Excel(List<XlsDto>xls)throwsException{
//获取总列数
intCountColumnNum=xls.size();
//创建Excel文档
HSSFWorkbookhwb=newHSSFWorkbook();
XlsDtoxlsDto=null;
//sheet对应一个工作页
HSSFSheetsheet=hwb.createSheet("pldrxkxxmb");
HSSFRowfirstrow=sheet.createRow(0);//下标为0的行开始
HSSFCell[]firstcell=newHSSFCell[CountColumnNum];
String[]names=newString[CountColumnNum];
names[0]="学号";
names[1]="姓名";
names[2]="学院";
names[3]="课程名";
names[4]="成绩";
for(intj=0;j<CountColumnNum;j++){
firstcell[j]=firstrow.createCell(j);
firstcell[j].setCellValue(newHSSFRichTextString(names[j]));
}
for(inti=0;i<xls.size();i++){
//创建一行
HSSFRowrow=sheet.createRow(i+1);
//得到要插入的每一条记录
xlsDto=xls.get(i);
for(intcolu=0;colu<=4;colu++){
//在一行内循环
HSSFCellxh=row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCellxm=row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCellyxsmc=row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCellkcm=row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCellcj=row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
//创建文件输出流,准备输出电子表格
OutputStreamout=newFileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("数据库导出成功");
}

}


XlsDto.java类

//该类是一个实体类

publicclassXlsDto{
/**
*选课号
*/
privateIntegerxkh;
/**
*学号
*/
privateStringxh;
/**
*姓名
*/
privateStringxm;
/**
*学院
*/
privateStringyxsmc;
/**
*课程号
*/
privateIntegerkch;
/**
*课程名
*/
privateStringkcm;
/**
*成绩
*/
privatefloatcj;
publicIntegergetXkh(){
returnxkh;
}
publicvoidsetXkh(Integerxkh){
this.xkh=xkh;
}
publicStringgetXh(){
returnxh;
}
publicvoidsetXh(Stringxh){
this.xh=xh;
}
publicStringgetXm(){
returnxm;
}
publicvoidsetXm(Stringxm){
this.xm=xm;
}
publicStringgetYxsmc(){
returnyxsmc;
}
publicvoidsetYxsmc(Stringyxsmc){
this.yxsmc=yxsmc;
}
publicIntegergetKch(){
returnkch;
}
publicvoidsetKch(Integerkch){
this.kch=kch;
}
publicStringgetKcm(){
returnkcm;
}
publicvoidsetKcm(Stringkcm){
this.kcm=kcm;
}
publicfloatgetCj(){
returncj;
}
publicvoidsetCj(floatcj){
this.cj=cj;
}

}


后台输出:

数据库导出成功
1.0hongten信息技术学院计算机网络应用基础80.0
2.0王五信息技术学院计算机网络应用基础81.0
3.0李胜基信息技术学院计算机网络应用基础82.0
4.0五班古信息技术学院计算机网络应用基础83.0
5.0蔡诗芸信息技术学院计算机网络应用基础84.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: