controller
@PostMapping("/import")
public AjaxResult import(MultipartFile file) {
PlantExcelImporter plantExcelImporter = new PlantExcelImporter(ptPlanService);
EasyExcelUtil.save(file, plantExcelImporter, PtPlanExcel.class);
return AjaxResult.success("导入成功");
}
PlantExcelImporter
import com.bjsasc.transferasset.adjust.domain.PtPlanAdjustExcel;
import com.bjsasc.transferasset.adjust.service.IPtPlanAdjustService;
import com.bjsasc.transferasset.utils.ExcelImporter;
import java.util.List;
public class PlantExcelImporter implements ExcelImporter<PtPlanAdjustExcel> {
private IPtPlanAdjustService ptPlanAdjustService;
public PlantAdjustExcelImporter(IPtPlanAdjustService ptPlanAdjustService) {
this.ptPlanAdjustService = ptPlanAdjustService;
}
@Override
public void save(List<PtPlanAdjustExcel> datas) {
ptPlanAdjustService.savePlanAdjustExcel(datas);
}
}
- 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
IPtPlanAdjustService 实现类
@Transactional(noRollbackFor = EasyExcelException.class, rollbackFor = Exception.class)
public void savePlanAdjustExcel(List<PtPlanExcel> ptPlanExcels) {
msgMap.put("countMsg", "导入总计:" + ptPlanAdjustExcels.size() + "条。");
msgMap.put("successMsg", "成功:" + PtPlanAdjustSet.size() + "条。");
msgMap.put("errorCount", "失败:" + errorNum + "条。");
msgMap.put("errorMsg", sb.toString());
msgMap.put("warningMsg", sbb.toString());
ptPlanAdjustMapper.batchSave(PtPlanAdjustSet);
throw new EasyExcelException(JSON.toJSONString(msgMap));
}
ExcelDataListener
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import java.util.List;
public class ExcelDataListener<T> extends AnalysisEventListener<T> {
private static final int BATCH_COUNT = 100000;
private List<T> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
private final ExcelImporter<T> importer;
public ExcelDataListener(ExcelImporter<T> importer) {
this.importer = importer;
}
@Override
public void invoke(T data, AnalysisContext analysisContext) {
cachedDataList.add(data);
if (cachedDataList.size() >= BATCH_COUNT) {
importer.save(cachedDataList);
cachedDataList.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
importer.save(cachedDataList);
cachedDataList.clear();
}
}
- 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
EasyExcelUtil
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.commons.codec.Charsets;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class EasyExcelUtil {
public static <T> void save(MultipartFile excel, ExcelImporter<T> importer, Class<T> clazz) {
ExcelDataListener<T> importListener = new ExcelDataListener<>(importer);
ExcelReaderBuilder builder = getReaderBuilder(excel, importListener, clazz);
if (builder != null) {
builder.doReadAll();
}
}
public static <T> List<T> read(MultipartFile excel, Class<T> clazz) {
DataListener<T> dataListener = new DataListener<>();
ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
if (builder == null) {
return null;
}
builder.doReadAll();
return dataListener.getDataList();
}
public static <T> List<T> read(MultipartFile excel, int sheetNo, int headRowNumber, Class<T> clazz) {
DataListener<T> dataListener = new DataListener<>();
ExcelReaderBuilder builder = getReaderBuilder(excel, dataListener, clazz);
if (builder == null) {
return null
- 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