easypoi实现快速导出

导入依赖
<!--EasyPoi-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.0.3</version>
</dependency>

避免重复代码,做一个工具类

public class ExcelUtil {

    public static void addExcelList(List<Map<String, Object>> excelList, ExportParams exportParams, Class<?> cls, List<?> list) {
        HashMap<String, Object> reportMap = new HashMap<>(16);
        reportMap.put("title", exportParams);
        reportMap.put("entity", cls);
        reportMap.put("data", list);
        excelList.add(reportMap);
    }

    public static KeyValue getExcelStream(HttpServletResponse response, Workbook workbook) throws IOException {
        try (ServletOutputStream out = response.getOutputStream()) {
            workbook.write(out);
            return KeyValue.ok("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException(e);
        }
    }
}

相关实现:

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService excelServiceImpl;

    @GetMapping("/export")
    public KeyValue excelExport(@Validated  PageForm form, HttpServletResponse response, BindingResult bindingResult)throws IOException {
        String fileName = new String("excel导出_".getBytes("UTF-8"),"ISO-8859-1");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename= "+fileName+ LocalDate.now().toString()+".xls");
        Workbook workbook = ExcelExportUtil.exportExcel(excelServiceImpl.getExcelExport(form), ExcelType.HSSF);
        workbook.setSheetName(0, "sheet名字");
        return ExcelUtil.getExcelStream(response, workbook);
    }

}
@Service
public class ExcelServiceImpl implements ExcelService {

    @Override
    public List<Map<String, Object>> getExcelExport(PageForm form) {
        ExportParams exportParams = new ExportParams();
        List<Map<String, Object>> excelList = new ArrayList<>();
        ExcelExportVo demo1 = ExcelExportVo.builder().name("测试1").date(new Date()).sex(1).build();
        ExcelExportVo demo2 = ExcelExportVo.builder().name("测试2").date(new Date()).sex(2).build();
        ExcelUtil.addExcelList(excelList, exportParams, ExcelExportVo.class, Lists.newArrayList(demo1,demo2));
        return excelList;
    }
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExcelExportVo {

    @Excel(name = "姓名")
    private String name;

    @Excel(name = "性别",replace = {"男_1","女_2"})
    private Integer sex;

    @Excel(name = "日期", format = "yyyy-MM-dd")
    private Date date;
}

其他easypoi用法:http://easypoi.mydoc.io/

 

Easypoi是一个Java的Excel导出工具库,可以帮助开发者快速导出大量数据到Excel文件中。下面是使用Easypoi进行快速导出100万条数据的步骤: 1. 引入Easypoi库:在项目的pom.xml文件中添加Easypoi的依赖项,或者将Easypoi的jar包导入到项目中。 2. 创建数据模型:定义一个Java类来表示要导出的数据模型,该类的属性对应Excel表格的列。 3. 准备数据:生成100万条数据,并将数据填充到数据模型对象中。 4. 创建Excel导出工具类:创建一个工具类,用于实现数据导出的逻辑。在该类中,使用Easypoi提供的API来创建Excel文档、设置表头、填充数据等操作。 5. 导出数据:调用工具类中的导出方法,将数据导出到Excel文件中。 下面是一个示例代码,演示了如何使用Easypoi快速导出100万条数据: ```java import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import org.apache.poi.ss.usermodel.Workbook; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; public class ExcelExportUtilDemo { public static void main(String[] args) throws Exception { // 准备数据 List<User> userList = generateData(1000000); // 创建Excel导出工具类 ExcelExportUtil<User> exportUtil = new ExcelExportUtil<>(User.class); // 导出数据 Workbook workbook = exportUtil.exportExcel(new ExportParams(), userList); // 保存Excel文件 FileOutputStream fos = new FileOutputStream("output.xlsx"); workbook.write(fos); fos.close(); } private static List<User> generateData(int count) { List<User> userList = new ArrayList<>(); for (int i = 1; i <= count; i++) { User user = new User(); user.setId(i); user.setName("User " + i); user.setAge(20 + i % 10); userList.add(user); } return userList; } // 定义数据模型类 public static class User { private int id; private String name; private int age; // 省略getter和setter方法 } } ``` 以上代码演示了如何使用Easypoi快速导出100万条数据到Excel文件中。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值