java导出excel(带标题,相同行合并的多个sheet表格。)

1.首先需要一个实体类
import lombok.Data;

@Data
public class PoiModel {
   
   
    //内容
    private String content;
    //上一行同一位置内容
    private String oldContent;
    //行标
    private int rowIndex;
    //列标
    private int cellIndex;
}
2.导出表格工具类
package com.ccb.financial.common.utils;


import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletResponse;

import com.ccb.financial.model.PoiModel;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.google.common.collect.Lists;

/**
 * 导出  动态合并单元格
 * @ClassName:ExportExcelByPoiUtil
 * @Description: 导出  动态合并单元格
 * @author: wjw
 * @date 2020年08月18日 下午3:22:27
 *
 */
public class ExportExcelByPoiUtil {
   
   
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    /**
     *
     * @Title: createExcel
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @author: wjw
     * @date: 2019年1月30日 下午4:37:01
     * @param @param request
     * @param @param response
     * @param @param title 标题数组
     * @param @param titleHead  Excel标题
     * @param @param widthAttr  单元格宽度
     * @param @param maps  数据
     * @param @param mergeIndex  要合并的列   数组
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    @SuppressWarnings("rawtypes")
    public static String createExcel(HttpServletResponse response,String[] title,String titleHead ,int[] widthAttr,Map<String/*sheet名*/, List<Map<String/*对应title的值*/, String>>> maps, int[] mergeIndex){
   
   
        String now = sdf.format(new Date());
        if (title.length==0){
   
   
            return null;
        }
        /*初始化excel模板*/
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = null;
        int n = 0;
        /*循环sheet页*/
        for(Map.Entry<String, List<Map<String/*对应title的值*/, String>>> entry : maps.entrySet()){
   
   
            /*实例化sheet对象并且设置sheet名称,book对象*/
            try {
   
   
                sheet = workbook.createSheet();
                workbook.setSheetName(n, entry.getKey());
                workbook.setSelectedTab(0);
            }catch (Exception e){
   
   
                e.printStackTrace();
            }
            // 设置样式 头 cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            // 水平方向的对齐方式
            CellStyle cellStyle_head = style(0, workbook);
            // 导出时间
            CellStyle cellStyle_export = style(3, workbook);
            // 标题
            CellStyle cellStyle_title = style(1, workbook);
            // 正文
            CellStyle cellStyle = style(2, workbook);
            // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
            CellRangeAddress c1 = new CellRangeAddress(0, 0, 0, title.length-1);
            sheet.addMergedRegion(c1);
            CellRangeAddress c2 = new CellRangeAddress(1, 1, 0, title.length-1);
            sheet.addMergedRegion(c2);
            // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
            Row row0 = sheet.createRow(0);
            // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
            Cell cell1 = row0.createCell(0);
            // 设置单元格内容 标题
            cell1.setCellValue(entry.getKey());
            cell1.setCellStyle(cellStyle_head);
            // 设置合并单元格边框
            setRegionStyle(sheet, c1, cellStyle_head);
            // 设置列宽
            for (int i = 0; i < widthAttr.length; i++) {
   
   
                sheet.setColumnWidth((short) i, (short) widthAttr[i] * 200);
            }
            // 在sheet里创建第二行
            Row row1 = sheet.createRow(1);
            // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
            Cell cell2 = row1.createCell(0);
            // 设置单元格内容 标题
            cell2.setCellValue("导出时间:" + now);
            cell2.setCellStyle(cellStyle_export);
            setRegionStyle(sheet, c2, cellStyle_export);
            /*初始化标题,填值标题行(第一行)*/
            Row row2 = sheet.createRow(2);
            for(int i = 0; i<title.length; i++){
   
   
                /*创建单元格,指定类型*/
                Cell cell_1 = row2.createCell(i, Cell.CELL_TYPE_STRING);
                //设置标题的值
                cell_1.setCellValue(title[i]);
                //设置标题样式
                cell_1.setCellStyle(cellStyle_title);
            }
            /*得到当前sheet下的数据集合*/
            List<Map<String/*对应title的值*/, String>> list 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值