java Excel导出工具类,半自动

一个导出excel的工具类,数据半自动,单元格设置值部分需要自己手动实现,这样更灵活一点,毕竟产品脑洞都比较大

package com.home.util;

/**
 * Excel类型
 */
public enum ExcelTypeEnum {
    XLS("xls", "2003"),
    XLSX("xlsx", "2010");
    String excelType;
    String desc;

    public String getExcelType() {
        return this.excelType;
    }

    ExcelTypeEnum(String excelType, String desc) {
        this.excelType = excelType;
        this.desc = desc;
    }
}
package com.home.util;

import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * 表格导出工具类,半自动
 *
 * @param <T>
 */
public abstract class ExportUtil<T> {
    private String fileType;
    private String xls = "xls";
    private String xlsx = "xlsx";

    //每个sheet默认6W条数据,自动拆分
    private Integer rowsize = 60000;

    private Workbook workbook;

    public void createWorkbook(ExcelTypeEnum type, String sheetName, List<Map<String, Integer>> headerList, List<T> bodyList, Integer rowsize) {
        this.rowsize = rowsize;
        this.createWorkbook(type, sheetName, headerList, bodyList);
    }

    public void createWorkbook(ExcelTypeEnum type, String sheetName, List<Map<String, Integer>> headerList, List<T> bodyList) {
        if (bodyList.size() < rowsize) {
            this.createSheet(type, sheetName, headerList, bodyList);
        } else {
            List<List<T>> partition = Lists.partition(bodyList, rowsize);
            for (int i = 0; i < partition.size(); i++) {
                List<T> list = partition.get(i);
                this.createSheet(type, sheetName + (i + 1), headerList, list);
            }
        }
    }

    private void createSheet(ExcelTypeEnum type, String sheetName, List<Map<String, Integer>> headerList, List<T> bodyList) {
        if (workbook == null) {
            if (ExcelTypeEnum.XLS.equals(type)) {
                workbook = new HSSFWorkbook();
            } else if (ExcelTypeEnum.XLSX.equals(type)) {
                workbook = new XSSFWorkbook();
            } else {
                throw new RuntimeException("格式不对");
            }
        }
        Sheet sheet = workbook.createSheet(sheetName);
        //创建头
        this.createHeader(sheet, headerList);
        //创建体
        this.createBody(workbook, sheet, bodyList, CollectionUtils.isEmpty(headerList) ? 0 : headerList.size());
    }

    private void createHeader(Sheet sheet, List<Map<String, Integer>> headerList) {
        if (CollectionUtils.isEmpty(headerList)) {
            return;
        }
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 12);
        CellStyle cellStyleHead = workbook.createCellStyle();
        cellStyleHead.setAlignment(HorizontalAlignment.CENTER);
        cellStyleHead.setFont(font);
        for (int i = 0; i < headerList.size(); i++) {
            Map<String, Integer> header = headerList.get(i);
            Row row = sheet.createRow(i);
            int k = 0;
            for (String key : header.keySet()) {
                Cell cell = row.createCell(k);
                cell.setCellValue(key);
                cell.setCellStyle(cellStyleHead);
                sheet.setColumnWidth(k, header.get(key));
                k++;
            }
        }
    }

    public abstract void createBody(Workbook workbook, Sheet sheet, List<T> bodyList, Integer headerListSize);

    public void out(OutputStream out) throws IOException {
        workbook.write(out);
        out.flush();
        out.close();

    }

    public static void main(String[] args) throws IOException {
        //表格数据信息
        List<Map<String, Object>> bodyList = new ArrayList<>();
        Map<String, Object> data1 = new HashMap<>();
        data1.put("param1", "a");
        data1.put("param2", "b");
        bodyList.add(data1);
        //构建头信息,支持多行
        List<Map<String, Integer>> headerList = new ArrayList<>();
        Map<String, Integer> header = new LinkedHashMap<>();
        header.put("Param1", 25 * 256);
        header.put("Param2", 25 * 256);
        headerList.add(header);
        //创建工具,需要泛型,单元格设置需要自己手动实现
        ExportUtil<Map<String, Object>> util = new ExportUtil<Map<String, Object>>() {
            @Override
            public void createBody(Workbook workbook, Sheet sheet, List<Map<String, Object>> bodyList, Integer headerListSize) {
                for (int i = 0; i < bodyList.size(); i++) {
                    Row row = sheet.createRow(i + headerListSize);
                    int k = -1;
                    //单元格设置自己手动实现,更灵活一点
                    Map<String, Object> details = bodyList.get(i);
                    row.createCell(++k).setCellValue((String) details.get("param1"));
                    row.createCell(++k).setCellValue((String) details.get("param2"));
                }
            }
        };
        //生成表格
        util.createWorkbook(ExcelTypeEnum.XLSX, "sheetname", headerList, bodyList);
        File file = new File("/Users/qishi/Desktop/aaaa.xlsx");
        OutputStream out = new FileOutputStream(file);
        //将表格输出到流
        util.out(out);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在写bug的路上越走越远

谢谢老板,老板发大财

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值