maven依赖
org.apache.poi poi 4.1.2 org.apache.poi poi-ooxml 4.1.2
ExcelUtils工具类,解析表格数据,不解析表头行
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtils {
public static List> getListByExcel(InputStream in, String fileName) throws Exception {
List> list = new ArrayList<>();
// 创建excel工作簿
Workbook work = getWorkbook(in, fileName);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
sheet = work.getSheetAt(0);
// 滤过第一行标题
for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null || row.getFirstCellNum() == j) {
continue;
}
List
controller代码
@PostMapping("/bulkImportAdsource")
public Response> bulkImportAdsource(MultipartHttpServletRequest request) throws Exception {
//前端传递参数名为 file parameter
MultipartFile file = request.getFile("file");
String parameter = request.getParameter("parameter");
AdSourceVo adSourceVo = JSON.parseObject(parameter, AdSourceVo.class);
if (file == null) {
return new Response<>(ResponseEnum.NON_File);
}
String fileName = file.getOriginalFilename();
assert fileName != null;
//解析表格数据,ExcelUtils工具类在上边
List> list = ExcelUtils.getListByExcel(file.getInputStream(), fileName);
String result = aggregateService.bulkImportAdsource(list,adSourceVo);
if (org.apache.commons.lang3.StringUtils.isBlank(result)){
return new Response<>(ResponseEnum.SUCCESS);
}else {
return new Response<>(ResponseEnum.PART_SUCCESS,result);
}
}
实现类代码
@Override public String bulkImportAdsource(List> list, AdSourceVo adSourceVo) { //封装要导入的代码位list List
sourceList = new ArrayList<>(); //遍历解析后的表格数据 for (int i = 1 ; i List



