java导出Excel(可导出大于6万条)

导出Excel

很多导出excel数据大于6万条就会500,经过修改此工具类大于6万条会从新生成个工作表,在下一个表中导出数据

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExcelUtil
 * @Description 导出excel
 * @Author 尚展锋
 * @Date 2020/5/16 13:25
 * @Version 1.0
 */
public class ExcelUtil {
    /**
     * 导出excle表
     */

    private static final String FILEPATH = PathKit.getWebRootPath() + File.separator + "upload" + File.separator ;
    public static String getTitle(){
        Date date = new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String title=FILEPATH+dateFormat.format(date)+"_统计报表.xls";
        return title;
    }

    public static File saveFile(Map<String, String> headData, List<Record> list, File file) {
        // 创建工作薄
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        // sheet:一张表的简称
        // row:表里的行
        // 创建工作薄中的工作表
        HSSFSheet hssfSheet = null;
        // 得到所有记录 行:列
        int index = 0;
        // 初始化索引 行
        int rowIndex = 0;
        // 初始化索引 列
        int cellIndex = 0;
        if (list != null) {
            // 获取所有的记录 有多少条记录就创建多少行
            for (int i = 0; i < list.size(); i++) {
                if(i == 0 || i%60000 == 0){
                    hssfSheet = hssfWorkbook.createSheet();
                    if(i != 0){
                        index++;
                    }
                    rowIndex = 0;
                    HSSFRow row = null;
                }
                // 创建行
                HSSFRow row = null;
                // 创建单元格,设置表头 创建列
                HSSFCell cell = null;
                if(rowIndex == 0){
                    // 创建标题行
                    row = hssfWorkbook.getSheetAt(index).createRow(rowIndex);
                    // 遍历标题
                    for (String h : headData.keySet()) {
                        //创建列
                        cell = row.createCell(rowIndex);
                        //索引递增
                        rowIndex++;
                        //逐列插入标题
                        cell.setCellValue(headData.get(h));
                    }
                    rowIndex=1;
                }

                row = hssfWorkbook.getSheetAt(index).createRow(rowIndex);
                // 得到所有的行 一个record就代表 一行
                Record  record = list.get(i);
                //下一行索引
                rowIndex++;
                //刷新新行索引
                cellIndex = 0;
                // 在有所有的记录基础之上,便利传入进来的表头,再创建N行
                for (String h : headData.keySet()) {
                    cell = row.createCell(cellIndex);
                    cellIndex++;
                    //按照每条记录匹配数据
                    cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
                }

            }
        }

        try {
            FileOutputStream fileOutputStreane = new FileOutputStream(file);
            hssfWorkbook.write(fileOutputStreane);
            fileOutputStreane.flush();
            fileOutputStreane.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
        SqlPara sqlPara= Db.getSqlPara("management.member.query");
        List<Record> list=Db.find(sqlPara);
        Map<String, String> titleData = new LinkedHashMap<>();
        //每个字段对应的标题
        titleData.put("id", "编号");
        titleData.put("user_name", "用户名");//※
        titleData.put("e_is_activate", "是否激活");
        titleData.put("end_time", "到期时间");
        titleData.put("register1", "注册渠道");
        titleData.put("standby2", "推荐渠道");
        titleData.put("act_time", "激活时间");
        titleData.put("add_time", "注册时间");
        File file = new File(ExcelUtil.getTitle());
        //调用导出excel方法
        file = ExcelUtil.saveFile(titleData, list, file);
        this.renderFile(file);
  
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值