给网站首页图片做外网超链接_为什么会弹出一个服务器登录窗口,网站建设公司厦门,装修室内效果图大全,华为云怎么建网站问题描述现需要批量导入数据#xff0c;数据以Excel形式导入。POI介绍我选择使用的是apache POI。这是有Apache软件基金会开放的函数库#xff0c;他会提供API给java#xff0c;使其可以对office文件进行读写。我这里只需要使用其中的Excel部分。实现首先#xff0c;Excel有…问题描述现需要批量导入数据数据以Excel形式导入。POI介绍我选择使用的是apache POI。这是有Apache软件基金会开放的函数库他会提供API给java使其可以对office文件进行读写。我这里只需要使用其中的Excel部分。实现首先Excel有两种格式一种是.xls(03版),另一种是.xlsx(07版)。针对两种不同的表格格式POI对应提供了两种接口。HSSFWorkbook和XSSFWorkbook导入依赖org.apache.poipoiRELEASEorg.apache.poipoi-ooxmlRELEASE处理版本Workbook workbook null;try {if (file.getPath().endsWith(xls)) {System.out.println(这是2003版本);workbook new XSSFWorkbook(new FileInputStream(file));} else if (file.getPath().endsWith(xlsx)){workbook new HSSFWorkbook(new FileInputStream(file));System.out.println(这是2007版本);}} catch (IOException e) {e.printStackTrace();}这里需要判断一下Excel的版本根据扩展名用不同的类来处理文件。获取表格数据获取表格中的数据分为以下几步1.获取表格2.获取某一行3.获取这一行中的某个单元格代码实现// 获取第一个张表Sheet sheet workbook.getSheetAt(0);// 获取每行中的字段for (int i 0; i sheet.getLastRowNum(); i) {Row row sheet.getRow(i); // 获取行// 获取单元格中的值String studentNum row.getCell(0).getStringCellValue();String name row.getCell(1).getStringCellValue();String phone row.getCell(2).getStringCellValue();}持久化获取出单元格中的数据后最后就是用数据建立对象了。List studentList new ArrayList();for (int i 0; i sheet.getLastRowNum(); i) {Row row sheet.getRow(i); // 获取行// 获取单元格中的值String studentNum row.getCell(0).getStringCellValue();String name row.getCell(1).getStringCellValue();String phone row.getCell(2).getStringCellValue();Student student new Student();student.setStudentNumber(studentNum);student.setName(name);student.setPhoneNumber(phone);studentList.add(student);}// 持久化studentRepository.saveAll(studentList);以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持聚米学院。