通过 Poi 操作 excel 文件,在拿到单元格的值,写入集合或者为对象属性赋值时,类型不匹配会提示:
java.lang.IllegalStateException: Cannot get a numeric value from a text cell
解决办法:
import org.apache.poi.ss.usermodel.DataFormatter;
FileInputStream fis = new FileInputStream(workbookName);
Workbook workbook = WorkbookFactory.create(fis);
Sheet sheet = workbook.getSheet(sheetName);
DataFormatter formatter = new DataFormatter();
String valueOfCell = formatter.formatCellValue(sheet.getRow(rowIndex).getCell(colIndex));
// 向 list 集合添加数据
list.add(valueOfCell);