java poi导入excel2013后台遇到的坑
错误1:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
该错误意思是说,文件中的数据是用Office2007+XML保存的,而现在却调用OLE2 Office文档处理,应该使用POI不同的部分来处理这些数据,比如使用XSSF来代替HSSF。
解决方案
第一步:
如果是是2013版及之前的,创建工作簿的时候使用:HSSFWorkbook workbook = new HSSFWorkbook();相应的之后在创建表格,行,单元格时使用:HSSFSheet sheet 、HSSFRow row、HSSFCell cell,
如果是是2017版,创建工作簿的时候使用: XSSFWorkbook workbook = new XSSFWorkbook();相应的之后在创建表格,行,单元格时使用:Sheet sheet、Row row、Cell cell。
第二步:
我们使用office 创建的是以 .xlsx 结尾的。但是要以 .xls 结尾的格式才能打开。
转换格式方法: 打开 .xlsx文件——>点击左上角的的文件里面的另存为选择你的要操作的文件名字打开找到选择保存类型里面你可以找到以 .xls结尾的文件即可,我选择的是excel 97-2003.xls这个。
错误2:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
No serializer found for class jee.entity.excelEntity and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])
意思说:转换为json时候出现了空值,
解决方案
在实体上面加入一个注解即可:
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
错误3:
java.lang.IllegalStateException: Cannot get a text value from a numeric cell
意思说:无法从数字单元格中获取文本值,就是说数字与字符串之间不能转换
解决方案
先设置Cell的类型,然后就可以把纯数字作为String类型读进来了:
把:
row.getCell(0).getStringCellValue();
换成:
if(row.getCell(0)!=null){
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
stuUser.setPhone(row.getCell(0).getStringCellValue());
}
该解决方案参考:https://blog.csdn.net/ysughw/article/details/9288307
最后我的代码
maven导入架包
官网:https://mvnrepository.com/里面有相对应的引入。
pol.xml 文件的引入
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.6</version>
</dependency>
三:执行代码
public void testReadList() throws Exception {
List<excelEntity> list = new ArrayList<excelEntity>();
HSSFWorkbook book = new HSSFWorkbook(new FileInputStream("d:/test/测试.xls"));
HSSFSheet sheet = book.getSheetAt(0);
for (int i = 2; i < sheet.getLastRowNum() + 1; i++) {
HSSFRow row = sheet.getRow(i);
String name = row.getCell(0).getStringCellValue(); // 名称
String url = row.getCell(1).getStringCellValue(); // url
String username = row.getCell(2).getStringCellValue();
String password = row.getCell(3).getStringCellValue();
Integer readCount = (int) row.getCell(4).getNumericCellValue();
list.add(new excelEntity(name, url, username, password, readCount));
}
System.out.println("共有 " + list.size() + " 条数据:");
for (excelEntity wd : list) {
System.err.println(wd);
}
}
实体:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class excelEntity {
private String name;
private String url;
private String username;
private String password;
private Integer readCount;
public excelEntity(String name, String url, String username, String password, Integer readCount) {
this.name = name;
this.url = url;
this.username = username;
this.password = password;
this.readCount = readCount;
}
public excelEntity() {}
@Override
public String toString() {
return "WebDto{" +
"name='" + name + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", readCount=" + readCount +
'}';
}
效果图:
执行结果:
功能实现我是参考:https://blog.csdn.net/zsl129/article/details/52960770/这位大佬的
欢迎前来指正