java web注解返回数据库_Java后台读取excel表格返回至Web前端

importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importentity.Bus;public classExcelTest {public static void main(String[] args) throwsIOException{/*** 读取表格

* 输出至前端*/Bus bus=null;

List list = new ArrayList<>();

String filePath="D://dns.xls";

InputStream input= newFileInputStream(filePath);

Workbook wb= null;

wb= newHSSFWorkbook(input);//得到一个工作表对象;

Sheet sheet = wb.getSheetAt(0);int rsRows = sheet.getLastRowNum();//获取sheet表中的总行数//遍历行//每一行成一个bus对象

for (int i=0;i<=rsRows;i++) {

Row row=sheet.getRow(i);int id=0;

String name=null;//遍历行单元格,已知有两列;第一列int型id,第二列String型name

Cell cell1 = row.getCell(0);

Cell cell2= row.getCell(1);

//一定要检验是否为空

if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){

break;

}else {

//数值型

id=(int) cell1.getNumericCellValue();

}

if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){

break;

}else {

//字符串型

name= cell2.getStringCellValue();

}bus=newBus(id,name);

list.add(bus);

System.out.print(id);

System.out.println(name);

}

wb.close();//记得关闭

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值