利用EasyPoi导出大量数据到Excel

利用EasyPoi导出大量数据到Excel

本项目是利用maven管理。
pom文件中EasyPoi的依赖为:

 <!--easypoi依赖-->
        <!-- 1.easypoi 父包作用大家都懂得
             2.easypoi-annotation 基础注解包,作用与实体对象上,拆分后方便maven多工程的依赖管理
             3.easypoi-base 导入导出的工具包,可以完成Excel导出,导入,Word的导出,Excel的导出功能
             4.easypoi-web 耦合了spring-mvc 基于AbstractView,极大的简化spring-mvc下的导出功能
             5.sax 导入使用xercesImpl这个包(这个包可能造成奇怪的问题哈),word导出使用poi-scratchpad,
             都作为可选包了
         -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.1</version>
        </dependency>
**实体类**
package com.cenhua.dataimport.pojo;

import cn.afterturn.easypoi.excel.annotation.Excel;

public class Customer implements java.io.Serializable{
    @Excel(name = "编号", width = 25)
     String id;

    @Excel(name = "姓名", width = 25)
     String name;

    @Excel(name = "性别", width = 10)
     String sex;

    @Excel(name = "年龄", width = 40)
     int age;
    @Excel(name = "n1", width = 25)
     String n1;

    @Excel(name = "n2", width = 25)
     String n2;

    @Excel(name = "n3", width = 10)
     String n3;

    @Excel(name = "n4", width = 40)
     int n4;

    @Excel(name = "n5", width = 25)
     int n5;

    @Excel(name = "n6", width = 25)
     String n6;

    @Excel(name = "n7", width = 10)
     String n7;

    @Excel(name = "n8", width = 40)
     String n8;
    @Excel(name = "n9", width = 25)
     String n9;

    @Excel(name = "n10", width = 25)
     String n10;

    @Excel(name = "n11", width = 10)
     String n11;

    @Excel(name = "n12", width = 40)
     String n12;

    @Excel(name = "n13", width = 10)
     String n13;

    @Excel(name = "n14", width = 40)
     String n14;

    @Excel(name = "n15", width = 40)
     String n15;

    //有参构造方法 Constructor
    public Customer(String id, String name, String sex, int age, String n1, String n2, String n3, int n4, int n5, String n6, String n7, String n8, String n9, String n10, String n11, String n12, String n13, String n14,String n15) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.n1 = n1;
        this.n2 = n2;
        this.n3 = n3;
        this.n4 = n4;
        this.n5 = n5;
        this.n6 = n6;
        this.n7 = n7;
        this.n8 = n8;
        this.n9 = n9;
        this.n10 = n10;
        this.n11 = n11;
        this.n12 = n12;
        this.n13 = n13;
        this.n14 = n14;
        this.n15 = n15;
    }
    public Customer(){
    }

    public String getN15() {
        return n15;
    }

    public void setN15(String n15) {
        this.n15 = n15;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public String getN1() {
        return n1;
    }

    public String getN2() {
        return n2;
    }

    public String getN3() {
        return n3;
    }

    public int getN4() {
        return n4;
    }

    public int getN5() {
        return n5;
    }

    public String getN6() {
        return n6;
    }

    public String getN7() {
        return n7;
    }

    public String getN8() {
        return n8;
    }

    public String getN9() {
        return n9;
    }

    public String getN10() {
        return n10;
    }

    public String getN11() {
        return n11;
    }

    public String getN12() {
        return n12;
    }

    public String getN13() {
        return n13;
    }

    public String getN14() {
        return n14;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setN1(String n1) {
        this.n1 = n1;
    }

    public void setN2(String n2) {
        this.n2 = n2;
    }

    public void setN3(String n3) {
        this.n3 = n3;
    }

    public void setN4(int n4) {
        this.n4 = n4;
    }

    public void setN5(int n5) {
        this.n5 = n5;
    }

    public void setN6(String n6) {
        this.n6 = n6;
    }

    public void setN7(String n7) {
        this.n7 = n7;
    }

    public void setN8(String n8) {
        this.n8 = n8;
    }

    public void setN9(String n9) {
        this.n9 = n9;
    }

    public void setN10(String n10) {
        this.n10 = n10;
    }

    public void setN11(String n11) {
        this.n11 = n11;
    }

    public void setN12(String n12) {
        this.n12 = n12;
    }

    public void setN13(String n13) {
        this.n13 = n13;
    }

    public void setN14(String n14) {
        this.n14 = n14;
    }

}

**控制层**
package com.cenhua.dataimport.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.export.ExcelBatchExportServer;
import com.cenhua.dataimport.pojo.Customer;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.*;
import java.util.*;


@Controller
@ExcelTarget("courseDomain")
public class DownloadController implements Serializable {
    @RequestMapping(value = "/downloadeasypoi")
    public void download(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        DownloadController d = new DownloadController();
        try {
            d.bigDataExport(httpServletRequest,httpServletResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建EXCEL文件
    public static Workbook creatExcel(List<List<String>> lists, String[] titles, String name) throws IOException {
        System.out.println(lists);
        //创建新的工作薄
        Workbook wb = new HSSFWorkbook();
        // 创建第一个sheet(页),并命名
        Sheet sheet = wb.createSheet(name);
        // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
        for(int i=0;i<titles.length;i++){
            sheet.setColumnWidth((short) i, (short) (35.7 * 150));
        }
        // 创建第一行
        Row row = sheet.createRow((short) 0);

        // 创建两种单元格格式
        CellStyle cs = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();

        // 创建两种字体
        Font f = wb.createFont();
        Font f2 = wb.createFont();

        // 创建第一种字体样式(用于列名)
        f.setFontHeightInPoints((short) 10);
        f.setColor(IndexedColors.BLACK.getIndex());
        f.setBoldweight(Font.BOLDWEIGHT_BOLD);

        // 创建第二种字体样式(用于值)
        f2.setFontHeightInPoints((short) 10);
        f2.setColor(IndexedColors.BLACK.getIndex());

        // 设置第一种单元格的样式(用于列名)
        cs.setFont(f);
        cs.setBorderLeft(CellStyle.BORDER_THIN);
        cs.setBorderRight(CellStyle.BORDER_THIN);
        cs.setBorderTop(CellStyle.BORDER_THIN);
        cs.setBorderBottom(CellStyle.BORDER_THIN);
        cs.setAlignment(CellStyle.ALIGN_CENTER);

        // 设置第二种单元格的样式(用于值)
        cs2.setFont(f2);
        cs2.setBorderLeft(CellStyle.BORDER_THIN);
        cs2.setBorderRight(CellStyle.BORDER_THIN);
        cs2.setBorderTop(CellStyle.BORDER_THIN);
        cs2.setBorderBottom(CellStyle.BORDER_THIN);
        cs2.setAlignment(CellStyle.ALIGN_CENTER);
        //设置列名
        for(int i=0;i<titles.length;i++){
            Cell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(cs);
        }
        if(lists == null || lists.size() == 0){
            return wb;
        }
        //设置每行每列的值
        for (int i1 = 1; i1 <= lists.size(); i1++) {
            // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
            // 创建一行,在页sheet上
            Row row1 = sheet.createRow(i1);
            for(int j1=0;j1<titles.length;j1++){
                // 在row行上创建一个方格
                Cell cell = row1.createCell(j1);
                cell.setCellValue(lists.get(i1-1).get(j1));
                cell.setCellStyle(cs2);
            }
        }
        return wb;
    }
    public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass,
                                          Collection<?> dataSet) {
        ExcelBatchExportServer batachServer = ExcelBatchExportServer
                .getExcelBatchExportServer(entity, pojoClass);
        return batachServer.appendData(dataSet);
    }

    public static void closeExportBigExcel() {
        ExcelBatchExportServer batachServer = ExcelBatchExportServer.getExcelBatchExportServer(null,
                null);
        batachServer.closeExportBigExcel();
    }
    public void bigDataExport(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // 告诉浏览器用什么软件可以打开此文件
        httpServletResponse.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        httpServletResponse.setHeader("Content-Disposition", "attachment;filename=user.xls");
        List<Customer> list = new ArrayList<Customer>();
        Workbook workbook = null;
        ExportParams params = new ExportParams("大数据测试", "测试");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
           Connection c = DriverManager.getConnection( "自己的url", "账号", "密码");
            String sql= "select * from xyz_copy1";
            Statement ps = c.createStatement();
            ResultSet rs = ps.executeQuery(sql);

            System.out.println("打印数据库数据");
            while(rs.next()) {
                Customer cu = new Customer();//创建对象 在循环内创建对象能够避免list添加值时会出现值被
                //map=new HashMap<String, Customer>();
                String id = rs.getString("id");
                String name =rs.getString("name");
                String sex =rs.getString("sex");
                int age = rs.getInt("age");
                String  n1 = rs.getString("n1");
                String n2 = rs.getString("n2");
                String n3 = rs.getString("n3");
                int n4 = rs.getInt("n4");
                int n5 = rs.getInt("n5");
                String n6 = rs.getString("n6");
                String n7 = rs.getString("n7");
                String n8 = rs.getString("n8");
                String n9 = rs.getString("n9");
                String n10 = rs.getString("n10");
                String n11 = rs.getString("n11");
                String n12 = rs.getString("n12");
                String n13 = rs.getString("n13");
                String n14 = rs.getString("n14");
                String n15 = rs.getString("n15");
                cu.setId(id);
                cu.setName(name);
                cu.setSex(sex);
                cu.setAge(age);
                cu.setN1(n1);
                cu.setN2(n2);
                cu.setN3(n3);
                cu.setN4(n4);
                cu.setN5(n5);
                cu.setN6(n6);
                cu.setN7(n7);
                cu.setN8(n8);
                cu.setN9(n9);
                cu.setN10(n10);
                cu.setN11(n11);
                cu.setN12(n12);
                cu.setN13(n13);
                cu.setN14(n14);
                cu.setN15(n15);
                list.add(cu);
                //在for循环之内  根据自己电脑可以设置读取条数
                if(list.size() == 10000){
                    workbook = ExcelExportUtil.exportBigExcel(params, Customer.class, list);
                    list.clear();
                }
            }
            workbook.write(httpServletResponse.getOutputStream());
            ExcelExportUtil.closeExportBigExcel();
            File savefile = new File("D:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream("D:/excel/ExcelExportBigData.bigDataExport.xls");
            workbook.write(fos);
            fos.close();
           } catch (Exception e) {
            e.printStackTrace();
        }

    }

}



//在for循环之内 根据自己电脑可以设置读取条数 此处为控制内存不溢出的关键
if(list.size() == 10000){
workbook = ExcelExportUtil.exportBigExcel(params, Customer.class, list);
list.clear();
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: easypoi是一款Java的POI扩展库,可以方便地将Java对象转换为Excel、Word、PDF等格式的文档。使用easypoi导出Excel大量数据非常简单,只需要将数据封装成Java对象,然后使用easypoi提供的API即可。同时,easypoi还支持Excel的样式设置、图片插入等功能,可以满足各种导出需求。 ### 回答2: Easypoi是一款基于POI的Java开发框架,它可以简化与Microsoft Excel文档交互的过程,提供了易于使用的API,使开发人员能够快速构建和导出Excel文档。 在导出大量数据的情况下,Easypoi的性能表现非常出色,它采用了多线程和文件流的方式进行数据的处理和输出,大大提高了导出效率,避免了出现内存溢出等问题。 Easypoi可以支持多种数据源的导出,包括List、Map、JavaBean等,开发人员可以根据具体业务需求选择合适的数据源进行导出。同时,Easypoi提供了丰富的样式设置和模板功能,通过对样式的设置和模板文件的使用,可以实现复杂数据导出。 在使用Easypoi进行大量数据导出的过程中,需要注意以下几点: 1. 数据的准备:在导出之前需要确保数据完整、正确。若数据源较大,可以考虑进行分页处理。 2. 导出样式的设置:Easypoi提供了多种样式设置的方法,如字体、颜色、边框等,可以通过设置样式让导出Excel文档更加美观。 3. 模板的设计与使用:如果需要导出数据比较复杂,可以考虑使用模板,将数据填充到模板中,模板文件中的样式和格式将会在导出文档中自动保留。 4. 文件流和多线程的使用:为了提高导出效率并避免内存溢出等问题,可以采用文件流和多线程的方式进行数据的处理和输出。 总之,Easypoi作为一款高效、易用的Excel导出框架,为我们提供了便利的开发和导出Excel文档的方法。在使用Easypoi导出大量数据时,需要注意一些细节,以确保导出的文档准确完整、美观清晰。 ### 回答3: EasyPoi是一款基于Apache POI封装的Java Excel操作工具,它提供了简单易用的API,使得导出Excel变得非常简单方便。在处理大量数据时,EasyPoi也是非常适用的。 首先,在导出大量数据时,需要使用PoiBaseView来创建Excel对象。它可以优化内存使用,避免内存溢出的问题。我们可以通过以下代码来创建PoiBaseView: ``` public class MyExcelView extends PoiBaseView { @Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { // 将数据写入Excel,以下省略 } } ``` 其次,我们需要设置分页导出,即每次导出指定数量的数据,以免一次性导出过多数据导致内存溢出。EasyPoi提供了一个非常方便的分页导出工具类,我们可以通过以下代码来设置分页: ``` ExportParams params = new ExportParams("标题", "表名"); params.setType(ExcelType.XSSF); // 设置Excel格式,XSSF为xlsx,HSSF为xls params.setSheetName("Sheet1"); // 设置Sheet名 params.setCreateHeadRows(true); // 设置是否创建头部信息 params.setFreezePane(1, 1); // 冻结第一行 params.setPageNum(10000); // 每次导出10000条数据 ``` 最后,我们需要配置导出数据的格式。EasyPoi提供了非常丰富的注解来配置导出数据格式,比如@Excel、@ExcelIgnore等。我们可以通过以下代码来配置导出数据格式: ``` public class User { @Excel(name = "ID", width = 25) private String id; @Excel(name = "姓名", width = 25) private String name; @Excel(name = "年龄", width = 25) private Integer age; //省略get/set方法 } ``` 以上是针对EasyPoi导出大量数据的简单介绍。EasyPoi提供了方便易用的接口,处理大量数据时也能得到非常好的性能表现。在实际应用中,我们可以根据具体需求进行更加详细的配置,以满足不同场景下的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值