【excel导出两级表头实现】

本文介绍了如何使用Java的POI库来实现Excel文件的两级表头导出,通过示例代码详细展示了具体实现过程。
摘要由CSDN通过智能技术生成

excel导出两级表头实现(poi)

java代码

@Getter
@Setter
public class ExcelHelper<T> {
   
    /**
     * 表格标题
     */
    private String title;

    /**
     * 单元格宽度
     */
    private int colWidth = 20;

    /**
     * 行高度
     */
    private int rowHeight = 20;

    private HSSFWorkbook workbook;

    /**
     * 表头样式
     */
    private HSSFCellStyle headStyle;

    /**
     * 主体样式
     */
    private HSSFCellStyle bodyStyle;

    /**
     * 日期格式化,默认yyyy-MM-dd HH:mm:ss
     */
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Constructor
     * @param title
     */
    public ExcelHelper(String title){
   
        this.title = title;
        workbook = new HSSFWorkbook();
        init();
    }
    /**
     * Constructor
     * @param title
     * @param colWidth
     * @param rowHeight
     */
    public ExcelHelper(String title, int colWidth, int rowHeight){
   
        this.colWidth = colWidth;
        this.rowHeight = rowHeight;
        this.title = title;
        workbook = new HSSFWorkbook();
        init();
    }

    /**
     * Constructor
     * @param title
     * @param colWidth
     * @param rowHeight
     * @param dateFormat
     */
    public ExcelHelper(String title, int colWidth, int rowHeight, String dateFormat) {
   
        this.title = title;
        this.colWidth = colWidth;
        this.rowHeight = rowHeight;
        workbook = new HSSFWorkbook();
        sdf = new SimpleDateFormat(dateFormat);
        init();
    }

    /**
     * 导出Excel,适用于web导出excel
     *
     * @param sheet
     * @param data
     */
    private void writeSheet(HSSFSheet sheet, List<T> data,List<Column> headerList) {
   
        try {
   
            sheet.setDefaultColumnWidth(colWidth);
            sheet.setDefaultRowHeightInPoints(rowHeight);
            createHead(headerList, sheet);
            writeSheetContent(headerList, data, sheet);
        } catch (Exception e) {
   
            throw new RuntimeException(e);
        }
    }

    /**
     * 导出表格
     * @param listColumn
     * @param datas
     * @return
     * @throws Exception
     */
    public InputStream exportExcel(List<Column> listColumn,List<T> datas) throws Exception {
   
        splitDataToSheets(datas,listColumn);
        return save(workbook);
    }

    /**
     * 导出表格 支持2级表头或单表头的Excel导出
     * @param headers
     * @param datas
     * @param filePath
     * @throws FileNotFoundException
     * @throws IOException
     * void
     */
    public void exportExcel(List<Column> headers,List<T> datas,String filePath) throws IOException {
   
        splitDataToSheets(datas, headers);
        save(workbook, filePath);
    }

    /**
     * 把数据写入到单元格
     * @param listColumn
     * @param datas
     * @param sheet
     * @throws Exception
     * void
     */
    private void writeSheetContent(List<Column> listColumn,List<T> datas,HSSFSheet sheet) throws Exception {
   
        HSSFRow row;
        List<Column> listCol = getColumnList(listColumn);
        for (int
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值