600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > poi java 导入excel_Java的poi技术读取和导入Excel

poi java 导入excel_Java的poi技术读取和导入Excel

时间:2023-12-18 18:57:17

相关推荐

poi java 导入excel_Java的poi技术读取和导入Excel

项目结构:

用到的Excel文件:

XlsMain .java 类

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

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

*

*参考地址:/blog/1160678

*

*/

publicclassXlsMain{

publicstaticvoidmain(String[]args)throwsIOException{

XlsMainxlsMain=newXlsMain();

XlsDtoxls=null;

Listlist=xlsMain.readXls();

try{

XlsDto2Excel.xlsDto2Excel(list);

}catch(Exceptione){

e.printStackTrace();

}

for(inti=0;i

xls=(XlsDto)list.get(i);

System.out.println(xls.getXh()+""+xls.getXm()+""

+xls.getYxsmc()+""+xls.getKcm()+""

+xls.getCj());

}

}

/**

*读取xls文件内容

*

*@returnList对象

*@throwsIOException

*输入/输出(i/o)异常

*/

privateListreadXls()throwsIOException{

InputStreamis=newFileInputStream("pldrxkxxmb.xls");

HSSFWorkbookhssfWorkbook=newHSSFWorkbook(is);

XlsDtoxlsDto=null;

Listlist=newArrayList();

//循环工作表Sheet

for(intnumSheet=0;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(版)中插入数据

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(Listxls)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

firstcell[j]=firstrow.createCell(j);

firstcell[j].setCellValue(newHSSFRichTextString(names[j]));

}

for(inti=0;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.0 hongten 信息技术学院 计算机网络应用基础 80.0

2.0 王五 信息技术学院 计算机网络应用基础 81.0

3.0 李胜基 信息技术学院 计算机网络应用基础 82.0

4.0 五班古 信息技术学院 计算机网络应用基础 83.0

5.0 蔡诗芸 信息技术学院 计算机网络应用基础 84.0

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。