springboot集成easyExcel导入excel
程序员文章站
2024-03-20 14:51:04
...
easyExcel需要的jar包
<!-- easyexcel 主要依赖 这一个基本上就够了-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
</dependency>
//导入excel
@RequestMapping(value = "excelImport", method = {RequestMethod.GET, RequestMethod.POST })
public void excelImport(HttpServletRequest request, @RequestParam("file") MultipartFile files) throws Exception {
MultipartFile file = files;
Map<String,Object> result = EasyExcelUtil.readExcel(file, new GybPatient(),1);
patientInfoService.excelImport(result);
}
package com.picc.config;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
public class EasyExcelUtil {
/**
* 读取某个 sheet 的 Excel
*
* @param excel 文件
* @param rowModel 实体类映射,继承 BaseRowModel 类
* @return Excel 数据 list
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel) throws IOException {
return readExcel(excel, rowModel, 1, 1);
}
/**
* 读取某个 sheet 的 Excel
* @param excel 文件
* @param rowModel 实体类映射,继承 BaseRowModel 类
* @param sheetNo sheet 的序号 从1开始
* @return Excel 数据 list
*/
public static Map<String,Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) throws IOException {
Map<String,Object> result = new HashMap<>();
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
reader.read(new Sheet(sheetNo, 0, rowModel.getClass()));
//校验表头
Boolean flag = false;
if(excelListener.getImportHeads().equals(excelListener.getModelHeads())){
flag = true;
}
result.put("flag", flag);
result.put("datas", excelListener.getDatas());
return result;
}
/**
* 读取某个 sheet 的 Excel
* @param excel 文件
* @param rowModel 实体类映射,继承 BaseRowModel 类
* @param sheetNo sheet 的序号 从1开始
* @param headLineNum 表头行数,默认为1
* @return Excel 数据 list
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) throws IOException {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass()));
return excelListener.getDatas();
}
/**
* 读取指定sheetName的Excel(多个 sheet)
* @param excel 文件
* @param rowModel 实体类映射,继承 BaseRowModel 类
* @return Excel 数据 list
* @throws IOException
*/
public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel,String sheetName) throws IOException {
ExcelListener excelListener = new ExcelListener();
ExcelReader reader = getReader(excel, excelListener);
if (reader == null) {
return null;
}
for (Sheet sheet : reader.getSheets()) {
if (rowModel != null) {
sheet.setClazz(rowModel.getClass());
}
//读取指定名称的sheet
if(sheet.getSheetName().contains(sheetName)){
reader.read(sheet);
break;
}
}
return excelListener.getDatas();
}
/**
* 返回 ExcelReader
* @param excel 需要解析的 Excel 文件
* @param excelListener new ExcelListener()
* @throws IOException
*/
private static ExcelReader getReader(MultipartFile excel,ExcelListener excelListener) throws IOException {
String filename = excel.getOriginalFilename();
if(filename != null && (filename.toLowerCase().endsWith(".xls") || filename.toLowerCase().endsWith(".xlsx"))){
InputStream is = new BufferedInputStream(excel.getInputStream());
return new ExcelReader(is, null, excelListener, false);
}else{
return null;
}
}
}
public void excelImport(Map<String, Object> result) {
Boolean flag = (Boolean) result.get("flag");
// if(flag){
List<Object> list = (List<Object>) result.get("datas");
System.out.println(list);
if(list != null && list.size() > 0){
for(Object o : list){
GybPatient xfxx = (GybPatient) o;
xfxx.setBalance("400");
xfxx.setCreateUser("张崇俊");
xfxx.setCreatedate(new Date());
try {
gybPatientMapper.insertPatient(xfxx);
}catch (Exception e){
}
}
}
}
上一篇: 事件响应链
下一篇: Python接口下载文件
推荐阅读
-
Springboot中使用easyExcel动态生成excel
-
springboot集成easyExcel导入excel
-
springboot使用EasyExcel压缩成zip导出多个excel
-
掌财社:Java如何使用EasyExcel实现导入导出excel
-
SpringBoot图文教程14—SpringBoot集成EasyExcel「上」
-
springboot使用easyexcel导出excel(使用converter转换Integer成String)
-
java使用EasyExcel导入导出excel
-
Springboot上传excel并将表格数据导入或更新mySql数据库的过程
-
Springboot上传excel并将表格数据导入或更新mySql数据库的过程
-
【springboot+easypoi】一行代码搞定excel导入导出