匯出檔案:使用Hutool匯出資料為Excel檔案

2022-01-04 14:00:02

背景

日常工作中,曾遇到過匯出資料為 Excel 的需求,這裡做個簡單總結。

相對於匯出檔案為 PDF 或者 Word ,匯出 Excel 相對更常用。

在實際中,遇到有的專案中使用前端外掛匯出 Excel 的方式,當資料量比較大時,對使用者端要求比較高,匯出很慢,影響使用者體驗。另外一種是今天這裡介紹的後端直接查詢、封裝、匯出為 Excel 檔案。

涉及的技術有: SpringBootMyBatishutool ,使用 hutool 工具匯出資料為 Excel

依賴

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.6</version>
</dependency>

<!--Export as Excel-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

核心匯出介面

/**
 * @Author Heartsuit
 * @Date 2021-08-09
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 匯出全量資料,實際一般為條件檢索後匯出
     * @param response
     * @throws IOException
     */
    @GetMapping("export-xls")
    public void exportExcel(HttpServletResponse response) throws IOException, ClassNotFoundException {
        ExcelWriter writer = ExcelUtil.getWriter();
        List<Employee> employees = employeeService.findAll();

        List<Map<String, Object>> rows = employees.stream().map(item -> {
            Map<String, Object> maps = new HashMap<>();
            maps.put("id", item.getId().toString());
            maps.put("name", item.getName());
            maps.put("age", item.getAge());
            maps.put("phone", item.getPhone());
            maps.put("createTime", item.getCreateTime().toString());
            return maps;
        }).collect(Collectors.toList());

        // Title
        int columns = Class.forName("com.heartsuit.springbootmybatis.oa.entity.Employee").getDeclaredFields().length;
        writer.merge(columns - 1, "員工資訊");

        // Header
        writer.addHeaderAlias("id", "ID");
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("age", "年齡");
        writer.addHeaderAlias("phone", "電話");
        writer.addHeaderAlias("createTime", "時間");

        // Body
        writer.setColumnWidth(0, 30);
        writer.setColumnWidth(1, 30);
        writer.setColumnWidth(2, 30);
        writer.setColumnWidth(3, 30);
        writer.setColumnWidth(4, 30);
        writer.write(rows, true);

        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("員工資訊表-" + DateUtil.today() + ".xls", "utf-8"));

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}

測試介面:全量匯出

GET http://localhost:8000/employee/export-xls

Note: 這裡使用 GET 方式,方便測試,實際建議 POST

測試1萬條資料匯出效率

  • 批次向資料表插入數萬條資料,再次測試匯出效率;
  • 其實,匯出時間取決於查效率以及查出的總資料量(涉及寫入Excel以及Excel傳輸兩部分時間);

批次寫入資料介面:

@Test
void insertBatch() {
    SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();

    //可以執行批次操作的sqlSession, try...with...
    try (SqlSession openSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
        long start = System.currentTimeMillis();
        EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
        for (int i = 0; i < 10000; i++) {
            Employee employee = new Employee();
            employee.setName(UUID.randomUUID().toString().substring(0, 6));
            employee.setAge(new Random().nextInt(100));
            employee.setPhone(MobileNumber.generate(0));
            mapper.save(employee);
        }
        openSession.commit();
        long end = System.currentTimeMillis();
        System.out.println("執行時長" + (end - start));
    }
}

從數萬條記錄中匯出1萬條資料,秒級。

匯出效果

2022-01-03-ExportExcel.jpg

Source Code

完整原始碼見 GitHubhttps://github.com/heartsuit/demo-spring-boot/tree/master/springboot-mybatis


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!