导出excel工具类

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
 * @Description 生成Excel工具类
 * @Author sinJack
 */
@Slf4j
public class ExcelUtils {
    /**
     * 生成Excel
     * @Params titleName 标题名称;columnName 列名称;data 导出数据集
     * @Return java.io.InputStream
     */
    public InputStream createExcel(String titleName,List<String> columnName,List<List<Object>> data) {
        // 生成文件
        ByteArrayOutputStream out = null;
        ExcelWriter writer = null;
        InputStream input = null;
        try {
            out = new ByteArrayOutputStream();
            // 通过工具类创建writer,生成xlsx格式
            writer = ExcelUtil.getWriter(true);
            //设置标题样式
            Font font1 = writer.createFont();
            font1.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
            CellStyle headCellStyle = writer.getHeadCellStyle();
            headCellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
            headCellStyle.setFont(font1);
            //设置标题,列名
            createTitle(writer,titleName,columnName);
            writer.passCurrentRow();
            writer.passCurrentRow();
            writer.passCurrentRow();
            writer.write(data, false);
            writer.autoSizeColumnAll();
            // 设置样式
            this.setCellStyle(writer, data);
            this.setSizeColumn(writer.getSheet(), data.size());
            //out为OutputStream,需要写出到的目标流
            writer.flush(out);
            input = new ByteArrayInputStream(out.toByteArray());
        } catch (Exception e) {
            log.error("生成文件异常:", e);
            return null;
        } finally {
            IoUtil.close(writer);
            IoUtil.close(out);
            IoUtil.close(input);
        }
        return input;
    }

    /**
     *  创建excel,设置标题,列名
     * @Params [writer, titleName, columnName]
     * @Return void
     */
    private void createTitle(ExcelWriter writer,String titleName,List<String> columnName) {
        writer.merge(0, 0, 0, columnName.size()-1, titleName, true);
        for(int i=0;i<columnName.size();i++){
            writer.merge(1,2,i,i,columnName.get(i),true);
        }
    }

    /**
     * 设置样式
     *
     * @param writer
     * @param rows
     */
    private void setCellStyle(ExcelWriter writer, List<List<Object>> rows) {
        //设置内容字体
        Font font1 = writer.createFont();
        font1.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        CellStyle headCellStyle = writer.getHeadCellStyle();
        headCellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
        headCellStyle.setFont(font1);
    }

    /**
     * 自适应宽度(中文支持)
     *
     * @param sheet
     * @param size
     */
    private void setSizeColumn(Sheet sheet, int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row currentRow;
                // 当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }

                if (currentRow.getCell(columnNum) != null) {
                    Cell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == CellType.STRING) {
                        int length = currentCell.getStringCellValue().getBytes(StandardCharsets.UTF_8).length;
                        if (columnWidth < length) {
                            columnWidth = length;
                        }
                    }
                }
            }
            int width = columnWidth * 256 > 255 * 256 ? 250 * 256 : columnWidth * 256;
            sheet.setColumnWidth(columnNum, width);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sinJack

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值