1. 程式人生 > >springboot2整合easypoi,實現Excel匯入匯出

springboot2整合easypoi,實現Excel匯入匯出

1.新增Maven依賴

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.2.0</version>
</dependency>

<!-- lombok可使程式碼更簡潔 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>

2.自定義EasyPoiUtils工具類

public final class EasyPoiUtils {

    private EasyPoiUtils() {}

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new  RuntimeException(e);
        }
    }

    private static<T> void defaultExport(List<T> dataList, Class<?> clz, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clz, dataList);
        if (workbook != null) {
            downLoadExcel(fileName, response, workbook);
        }
    }

    public static<T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, boolean isCreateHeader, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(dataList, clz, fileName, response, exportParams);
    }

    public static<T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, HttpServletResponse response) {
        defaultExport(dataList, clz, fileName, response, new ExportParams(title, sheetName));
    }

    private static void defaultExport(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(dataList, ExcelType.HSSF);
        if (workbook != null) {
            downLoadExcel(fileName, response, workbook);
        }
    }

    public static void exportExcel(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {
        defaultExport(dataList, fileName, response);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> clz) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }

        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);

        try {
            return ExcelImportUtil.importExcel(new File(filePath), clz, params);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> clz) {
        if (file == null) {
            return null;
        }

        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);

        try {
            return ExcelImportUtil.importExcel(file.getInputStream(), clz, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static List<ExportUser> importExcel(MultipartFile file, Class<ExportUser> clz) {
        if (file == null) {
            return null;
        }

        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        try {
            return ExcelImportUtil.importExcel(file.getInputStream(), clz, params);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

3.Excel匯入

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExportUser {
    @Excel(name = "使用者名稱", width = 10)
    private String username;

    @Excel(name = "建立時間", format = "yyyy-MM-dd", width = 15)
    private Date createTime;
}

@RequestMapping("/import/users")
@ResponseBody
public List<ExportUser> importUsers(@RequestParam MultipartFile file) {
    return EasyPoiUtils.importExcel(file, ExportUser.class);
}

4.Excel匯出

@RequestMapping(value = "/export/users", method = RequestMethod.GET)
public void exportUsers(HttpServletResponse response) {
    List<ExportUser> userList = getUserList();
    EasyPoiUtils.exportExcel(userList, "使用者列表", "使用者報表", ExportUser.class, "使用者明細報表.xls", response);
}

private List<ExportUser> getUserList() {
    List<ExportUser> userList = new ArrayList<>();
    userList.add(new ExportUser("tom", new Date()));
    userList.add(new ExportUser("jack", new Date()));
    userList.add(new ExportUser("123", new Date()));
    return userList;
}

5.測試

匯出:http://localhost:8080/demo/export/users
匯入:http://localhost:8080/demo/import/users (用postman或者curl測試吧)

原始碼 https://gitee.com/jsjack_wang/springboot-demo dev-easypoi分支