上图片
原本以为转换问题最后发现依赖问题
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
去掉 poi这个依赖就好了2者起冲突
去掉后
解决!!!
附上代码
//测试类
@SpringBootTest
class TextDemoApplicationTests {
@Autowired
private StudetService studetService;
@Test
public void demo() throws FileNotFoundException {
File file = new File("D:\\ExcelStu.xlsx");
// 写法1:JDK8+ ,不用额外写一个DemoDataListener TestFileUtil.getPath() +
// since: 3.0.0-beta1
//String fileName = "demo" + File.separator + "demo.xlsx";
FileInputStream fileInputStream = new FileInputStream(file);
// 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
// 具体需要返回多少行可以在`PageReadListener`的构造函数设置
EasyExcel.read(fileInputStream, ExcelStudent.class, new DemoDataListener(studetService)
).headRowNumber(1).sheet().doRead();
}
}
//监听
/**
* @author 在偏一点就进了
* @version 1.0
* @date 2023/4/3 11:05
* @description
*/
@Slf4j
public class DemoDataListener implements ReadListener<ExcelStudent> {
/**
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 100;
private StudetService studetService;
/**
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
*
* @param studetService
*/
public DemoDataListener(StudetService studetService) {
this.studetService = studetService;
}
/**
* 缓存的数据
*/
private List<ExcelStudent> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
@Override
public void invoke(ExcelStudent data, AnalysisContext analysisContext) {
log.info("参数:{}", JSON.toJSONString(data));
cachedDataList.add(data);
if (cachedDataList.size()>=BATCH_COUNT){
//存储逻辑
saveData();
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
saveData();
log.info("读取完毕!!");
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
studetService.save();
log.info("存储数据库成功!");
}
}
//domain层
/**
* @author 在偏一点就进了
* @version 1.0
* @date 2023/4/3 10:56
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelStudent {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private String age;
@ExcelProperty("生日")
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
//@ExcelIgnore
private String birthday;
}
//StudetService 层
/**
* @author 在偏一点就进了
* @version 1.0
* @date 2023/4/3 15:07
* @description
*/
@Service
public class StudetService {
// 插入逻辑
public String save(){
return "成功";
}
}
表格