千万级数据导出

2023-10-18 15:08:01 浏览数 (1)

代码语言:javascript复制
@RequestMapping("/excelTest.xlsx")
public void excelTest(HttpServletResponse response) throws Exception{
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    //responseHeader(response,"模板");
    long t1=System.currentTimeMillis();
    ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();

    for (int i = 1; i <= 10 ; i  ) {
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetNo(i);
        writeSheet.setSheetName("aa" i);
        excelWriter.write(data(),writeSheet);
    }
    excelWriter.finish();
    long t2=System.currentTimeMillis();
    System.out.println("t2-t1 = "   ((t2 - t1)/1000));
}

//应该查询数据
private static List<User> data() {
    List<User> list = new ArrayList<User>();
    for (int i = 1; i <= 1000000; i  ) {
        User user=new User();
        user.setId(i);
        user.setName("张三" i);
        user.setPwd("密码" i);
        list.add(user);
    }
    return list;
}

0 人点赞