poiexcel 读取引用列_java中使用poi导入Excel表格

@RequestMapping("/importExcel")

@ResponseBodypublic String importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse

response) {

System.out.println("file"+file.getSize());try{//@RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件//1.创建workbook对象,读取整个文档

InputStream inputStream =file.getInputStream();//POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);

XSSFWorkbook wb = newXSSFWorkbook(inputStream);//2.读取页脚sheet

XSSFSheet sheetAt = wb.getSheetAt(0);//3.循环读取某一行

int index = 0;for(Row row : sheetAt) {//4.读取每一行的单元格

if (index == 0) {

index++;continue;

}

//创建一个学生对象

SysStudent student= newSysStudent();

//将Excel表中单元格的值与学生对象的值对应

student.setName(row.getCell(0).getStringCellValue());

//因为学号是数字,Excel默认是数字类型,我的数据库是字符串类型,所以需要设置下类型

row.getCell(1).setCellType(CellType.STRING);

student.setStuId(row.getCell(1).getStringCellValue());

student.setIdentity(row.getCell(2).getStringCellValue());

student.setDescription(row.getCell(3).getStringCellValue());

student.setProvince(row.getCell(4).getStringCellValue());

sysStudentService.insertSysStudent(student);

row.getCell(0).setCellType(CellType.STRING);

String stringCellValue= row.getCell(0).getStringCellValue();

row.getCell(1).setCellType(CellType.STRING);

String stringCellValue2= row.getCell(1).getStringCellValue();

row.getCell(2).setCellType(CellType.STRING);

String stringCellValue3= row.getCell(2).getStringCellValue();

row.getCell(3).setCellType(CellType.STRING);

String stringCellValue4= row.getCell(3).getStringCellValue();

row.getCell(4).setCellType(CellType.STRING);

String stringCellValue5= row.getCell(4).getStringCellValue();//写多少个具体看大家上传的文件有多少列.....//测试是否读取到数据,及数据的正确性

System.out.println(stringCellValue);

System.out.println(stringCellValue2);

System.out.println(stringCellValue3);

System.out.println(stringCellValue4);

System.out.println(stringCellValue5);

}

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}return "上传成功";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值