前言 :在开发的业务中,我们通常都会遇到有数据的导入导出相关功能,以下是一种Excel
实现导入相关历程
注:因为现在大多数都是使用的Spring Boot
微服务,使用Maven
作为Jar包仓库,以下代码都是在Spring Boot
环境下开发的
1. 准备
项目中添加相关的Excel
相关操作的Maven
依赖:
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.13</version> </dependency>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.13</version> </dependency>
|
2. 相关导入代码实现
控制层代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
@PostMapping("importExcel") public Response importExcel(MultipartFile file) throws IOException { if (!ExcelUtils.checkFile(file)){ return Response.initFail("文件格式无法通过"); } Workbook workbook = ExcelUtils.getWorkBook(file); List<Users> list = new ArrayList<>(); if (workbook != null) { for(int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); ++sheetNum) { Sheet sheet = workbook.getSheetAt(sheetNum); if ((sheet != null && sheet.getSheetName().equals("Users"))) { int firstRowNum = sheet.getFirstRowNum();
int lastRowNum = sheet.getLastRowNum();
for(int rowNum = firstRowNum + 1; rowNum <= lastRowNum; ++rowNum) { Row row = sheet.getRow(rowNum); if (row != null && !ExcelUtils.isRowEmpty(row)) { int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); if (lastCellNum > 0) { Users info = new Users(); for(int cellNum = firstCellNum; cellNum < lastCellNum; ++cellNum) { Cell cell = row.getCell(cellNum); if (cell != null) { cell.setCellType(1); switch(cellNum) { case 0: info.setUserName(cell.getStringCellValue()); break; case 1: info.setLoginName(cell.getStringCellValue()); break; case 2: info.setLoginPassword(cell.getStringCellValue()); break; case 3: info.setEmail(cell.getStringCellValue()); break; case 4: info.setPhone(Integer.parseInt(cell.getStringCellValue())); break; case 5: info.setAddress(cell.getStringCellValue()); break; case 6: info.setLoginDate(cell.getDateCellValue()); break; case 7: info.setLoginIp(cell.getStringCellValue()); } } } list.add(info); } } } } } }
return null; }
|
3. 工具类 ExcelUtils的相关代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| package com.novel.common.poi;
import com.novel.common.core.utils.string.StringUtils; import com.novel.common.file.utils.file.FileTypeUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.InputStream;
public class ExcelUtils {
public static Workbook getWorkBook(MultipartFile file) { String fileName = file.getOriginalFilename(); Object workbook = null;
try { InputStream is = file.getInputStream(); if (fileName.endsWith("xls")) { workbook = new HSSFWorkbook(is); } else if (fileName.endsWith("xlsx")) { workbook = new XSSFWorkbook(is); } } catch (IOException var4) { var4.getMessage(); }
return (Workbook)workbook; }
public static boolean checkFile(MultipartFile file) throws IOException { if (null == file) { return false; }
String fileName = file.getOriginalFilename(); if (!fileName.endsWith("xls") && !fileName.endsWith("xlsx")) { return false; }else{ String fileType = FileTypeUtils.getFileType(file.getInputStream()); if (!fileType.equals("xls") && !fileType.equals("xlsx")){ return false; } } return true;
}
public static boolean isRowEmpty(Row row) { if (null == row) { return true; } else { int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); int nullCellNum = 0;
for(int c = firstCellNum; c < lastCellNum; ++c) { Cell cell = row.getCell(c); if (null != cell && 3 != cell.getCellType()) { cell.setCellType(1); String cellValue = cell.getStringCellValue().trim(); if (StringUtils.isEmpty(cellValue)) { ++nullCellNum; } } else { ++nullCellNum; } }
if (nullCellNum == lastCellNum - firstCellNum) { return true; } else { return false; } } } }
|