EasyPoi巨量資料匯入匯出百萬級範例

2022-11-07 12:00:09

EasyPoi介紹:

利用註解的方式簡化了Excel、Word、PDF等格式的匯入匯出,而且是百萬級資料的匯入匯出。EasyPoi官方網址:EasyPoi教學_V1.0 (mydoc.io)。下面我寫了一個測試用例,真的是很方便,可以利用註解自動完成單元格的合併,設定單元格寬度、設定字元替換、並且可以很好的完成實體類之間一對一、一對多關係的處理

不賣關子,事先說明百萬級巨量資料操作使用:匯入(importExcelBySax),匯出(exportBigExcel)

  • 匯入依賴
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.1.0</version>
</dependency>
  • 實體物件
/**
 * 免打擾手機號
 *
 * @author Mark [email protected]
 * @since 1.0.0
 */
@Data
public class NonIntrusiveExcel {
    @Excel(name = "手機號碼", width = 20)
    @NotNull
    private String phone;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NonIntrusiveExcel that = (NonIntrusiveExcel) o;
        return phone.equals(that.phone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(phone);
    }
}
  • 匯入匯出工具類
/**
 * excel工具類
 *
 * excel中xls和xlsx的區別是:
 * 1、檔案格式不同。xls是一個特有的二進位制格式,其核心結構是複合檔案型別的結構,而xlsx的核心結構是XML型別的結構,採用的是基於 XML 的壓縮方式,使其佔用的空間更小。xlsx 中最後一個 x 的意義就在於此。
 * 2、版本不同。xls是excel2003及以前版本生成的檔案格式,而xlsx是excel2007及以後版本生成的檔案格式。
 * 3、相容性不同。xlsx格式是向下相容的,可相容xls格式。
 *
 * @author Mark [email protected]
 */
public class ExcelUtils {

    /**
     * Excel匯入
     *
     * @param request       request
     * @param pojoClass     物件Class
     */
    public static List importExcel(HttpServletRequest request, Class<?> pojoClass) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            throw new RenException("未找到上傳的檔案!");
        }
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setNeedVerify(true); // 開啟校驗規則
        List targetList = null;

        try {
            System.out.println("正在讀取檔案: " + file.getOriginalFilename() + ",開始匯入資料。");
            targetList = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            file.getInputStream().close();
        }

        return targetList;
    }

    /**
     * Excel巨量資料匯入
     *
     * @param request       request
     * @param pojoClass     物件Class
     */
    public static Set importBigExcel(HttpServletRequest request, Class<?> pojoClass) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            throw new RenException("未找到上傳的檔案!");
        }
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setNeedVerify(true); // 開啟校驗規則
        Set targetList = new HashSet(); // 新增set集合過濾去重元素

        try {
            System.out.println("正在讀取檔案: " + file.getOriginalFilename() + ",開始匯入資料。");
            ExcelImportUtil.importExcelBySax(file.getInputStream(), pojoClass, params, new IReadHandler() {
                @Override
                public void handler(Object o) {
                    targetList.add(o);
                }

                @Override
                public void doAfterAll() {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            file.getInputStream().close();
        }

        return targetList;
    }

    /**
     * Excel匯出
     *
     * @param response      response
     * @param fileName      檔名
     * @param list          資料List
     * @param pojoClass     物件Class
     */
    public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list,
                                   Class<?> pojoClass) throws IOException {
        if (StringUtils.isBlank(fileName)) {
            //當前日期
            fileName = DateUtil.formatDatetime(new Date());
        }

        // 設定匯出格式為xlsx,預設xlsx
        ExportParams exportParams = new ExportParams();
        exportParams.setType(ExcelType.XSSF);

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel");
//        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    /**
     * Excel巨量資料匯出
     *
     * @param list          資料List
     * @param pojoClass     物件Class
     */
    public static byte[] exportBigExcelByte(Collection<?> list, Class<?> pojoClass) throws IOException {
        Workbook workbook;

        // 設定匯出單sheet頁最大一百萬行資料
        ExportParams exportParams = new ExportParams();
        exportParams.setMaxNum(1000000);
        exportParams.setType(ExcelType.XSSF);

        workbook = ExcelExportUtil.exportBigExcel(exportParams, pojoClass, (queryParams, num) -> {
            // 只匯出一次,第二次返回null終止迴圈
            if (((int) queryParams) == num) {
                return null;
            }
            System.out.println("正在進行巨量資料量匯出,條數: " + list.size());
            return Arrays.asList(list.toArray());
        }, 2);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        workbook.write(bos);
        bos.close();

        byte[] bytes = bos.toByteArray();

        return bytes;
    }

    /**
     * Excel匯出,先sourceList轉換成List<targetClass>,再匯出
     *
     * @param response      response
     * @param fileName      檔名
     * @param sourceList    原資料List
     * @param targetClass   目標物件Class
     */
    public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
                                           Class<?> targetClass) throws Exception {
        List targetList = new ArrayList<>(sourceList.size());
        for (Object source : sourceList) {
            Object target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
            targetList.add(target);
        }

        exportExcel(response, fileName, targetList, targetClass);
    }

    /**
     * Excel生成
     *
     * @param response      response
     * @param fileName      檔名
     */
    public static void mkdirExcel(HttpServletResponse response, String fileName,
                                  Workbook workbook) throws IOException {
        if (StringUtils.isBlank(fileName)) {
            //當前日期
            fileName = DateUtil.formatDatetime(new Date());
        }

        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    /**
     * XSSF
     * excel新增下拉資料校驗
     *
     * @param workbook   哪個 sheet 頁新增校驗
     * @param dataSource 資料來源陣列
     * @param col        第幾列校驗(0開始)
     * @return
     */
    public static void createXssfSelected(Workbook workbook, String[] dataSource, int col) {
        Sheet sheet = workbook.getSheetAt(0);
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, col, col);
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(dataSource);
        DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
        //處理Excel相容性問題
        if (dataValidation instanceof XSSFDataValidation) {
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        } else {
            dataValidation.setSuppressDropDownArrow(false);
        }
        dataValidation.setEmptyCellAllowed(true);
        dataValidation.setShowPromptBox(true);
        dataValidation.createPromptBox("提示", "只能選擇下拉框裡面的資料");
        sheet.addValidationData(dataValidation);
    }

}
  • 介面呼叫
@RestController
@RequestMapping("/app/nonIntrusive")
@Slf4j
public class NonIntrusiveController {
@Autowired
private NonIntrusiveService appNonIntrusiveService;

@PostMapping(value = "/importAndExportFilter")
@LogOperation("免打擾過濾")
@RequiresPermissions("app:nonIntrusive:filter")
public byte[] importFilter(HttpServletRequest request) throws Exception {
long startTime = System.currentTimeMillis();

// 讀取excel檔案內容轉成集合
Set<NonIntrusiveExcel> importSet = ExcelUtils.importBigExcel(request, NonIntrusiveExcel.class);

log.info("已讀取檔案內容條數:{}, 總共耗時:{} 毫秒", importSet.size(), System.currentTimeMillis() - startTime);

startTime = System.currentTimeMillis();

// 讀取資料表列表集合
Set<NonIntrusiveExcel> phoneSet = appNonIntrusiveService.listExcel();

log.info("已載入儲存的免打擾號碼庫總條數:{}, 總共耗時:{} 毫秒", phoneSet.size(), System.currentTimeMillis() - startTime);

startTime = System.currentTimeMillis();

// Set排除已存在的集合資料
importSet.removeAll(phoneSet);

log.info("已完成過濾免打擾號碼後的總條數:{}, 總共耗時:{} 毫秒", importSet.size(), System.currentTimeMillis() - startTime);

// 進行巨量資料excel匯出
return ExcelUtils.exportBigExcelByte(importSet, NonIntrusiveExcel.class);
}

@GetMapping(value = "/exportTemplate")
public void export(HttpServletResponse response) throws Exception {
ExcelUtils.exportExcelToTarget(response, null, new ArrayList<>(), NonIntrusiveExcel.class);
}

}