spring spring boot mybatis mysql Java使用POI从Excel读取数据,解决读取到空行问题,并存入数据库。 - getStringCellValue()

📅 2026-12-01 👤 拼多多助力营销技巧 📂 pdd帮助力 👁️ 533 次阅读

项目场景:需要从Excel读取多行数据,映射到java对象,并存入到数据库当中(单Sheet版)

问题分析

例如:读取数据映射到java实体类的时候,发现了有一条数据的属性都为null,导致一些方法的空指针异常。通过debug我们发现,实际上Excel当中数据有4条(包括header),但是在使用poi读取并映射到java实体类的时候却映射了5条。

解决方案:

既然最后一条数据读到的是没有数据的一行,我们就可以在遍历excel数据的时候加一条判断,每一行当中每一个单元格的通过poi读取后,在java中岗位cell类型的对象,只需判断是否为null,若为null直接结束遍历即可。

其中最外层循环,为遍历每行数据。内层循环为遍历每行数据当中的单元格Cell,内层循环的数量是根据第一行(header头部标题)的数量来决定的。

完整代码:

maven先导入poi相关jar包,本人使用的是 4.1.2 版本。

org.apache.poi

poi-ooxml

${poi.version}

org.apache.poi

poi

${poi.version}

org.apache.poi

poi-scratchpad

${poi.version}

该代码为单sheet版本,若为多sheet版本只需在外层加一层for循环遍历sheet即可。

若Excel表中数据为日期,可以使用cell.getDateCellValue()来获取,cell提供的方法很多,可以根据具体情况来进行获取。

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springblade.busi.entity.PersonalInfo;

import org.springblade.busi.framework.annotation.InfoChangeAnnotation;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class ExcelToObject1 {

/** 拼多多帮助力入口

* 读取Excel文件,版本:只读取基本信息一个Sheet,若有需要多个sheet在增加for循环

*

* @param file

* @return

*/

public ListtoPersonalInfo(MultipartFile file) {

//创建返回对象

Listpersons = new ArrayList<>();

try (InputStream fis = file.getInputStream();

Workbook workbook = new XSSFWorkbook(fis)) {

Class clazz = PersonalInfo.class;

//只读取基本信息一个Sheet

Sheet sheet = workbook.getSheetAt(0);

Row headerRow = sheet.getRow(0);

ListcolumnNames = new ArrayList<>();

IteratorcellIterator = headerRow.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

columnNames.add(cell.getStringCellValue());

}

for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {

Row row = sheet.getRow(rowIndex);

PersonalInfo person = (PersonalInfo) clazz.newInstance();

//获取Excel当前行的第一位,若未null则不处理 --->防止poi读取Excel多读取没有数据的一行,导致非空字段为空。

Cell cellNow = row.getCell(0);

if (cellNow == null) {

break;

}

for (int columnIndex = 0; columnIndex < columnNames.size(); columnIndex++) {

Cell cell = row.getCell(columnIndex);

String columnName = columnNames.get(columnIndex);

//通过实体类注解与Excel头部中文进行映射

Field field = getFieldByColumnName(clazz, columnName);

if (field != null) {

field.setAccessible(true);

field.set(person, getCellValueAsString(cell));

}

}

//处理每一行Excel加到返回对象当中

persons.add(person);

}

} catch (IOException | IllegalAccessException | InstantiationException e) {

e.printStackTrace();

System.out.println(e.getMessage());

}

return persons;

}

//通过实体类注解与Excel头部中文进行映射

private static Field getFieldByColumnName(Class clazz, String columnName) {

for (Field field : clazz.getDeclaredFields()) {

InfoChangeAnnotation annotation = field.getAnnotation(InfoChangeAnnotation.class);

if (annotation != null && annotation.value().equals(columnName)) {

return field;

}

}

return null;

}

//获取每个单元格数据并转为String,其他类型在上方做了ifelse

private static String getCellValueAsString(Cell cell) {

if (cell == null) {

return null;

}

cell.setCellType(CellType.STRING);

String cellValue = cell.getStringCellValue();

System.out.println("getCellValueAsString:" + cellValue);

return cellValue;

}

在映射java实体类的时候需要在其实体类属性添加注解,注解可以自行创建,也可以引用其他带有value的注解,本文章使用的是 @InfoChangeAnnotation("Excel表中第一行的中文")。直接可以与Excel表头进行映射。 上述代码中PersonalInfo为需要映射到java的实体类,必须根据实际情况进行修改。

@InfoChangeAnnotation("姓名")

private String personalName;

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧