【Java】poi | excel | 合并单元格

一、说明

        1、maven项目

        2、基于ruoyi-fast

二、解决方案

1、合并行

需求:合并第一行和第二行

解决:

CellRangeAddress region = new CellRangeAddress(0, 1, 0, 0);
sheet.addMergedRegion(region);

示例图:

解释1: firstRow,lastRow,即合并从第N行到第N行,从0开始

解释2: 合并行,列数不变

解释3: 合并行,需连续

2、合并列

需求: 合并第6列到第17列

解决:

region = new CellRangeAddress(0, 0, 5, 16);
sheet.addMergedRegion(region);

示例图:

解释1: firstCol,lastCol,即合并从第N列到第N列

解释2: 合并列,行数不变

解释3: 合并列,需连续

3、同时合并行合并列

请自由发挥

三、完成测试类

与 ExcelUtil 在同一包

package com.ruoyi.common.utils.poi;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * demo测试
 * @author hgSuper
 * @date 2021-08-09
 */
public class DemoExcelUtils {

    public static void main(String[] args)throws Exception {
        t();
    }
    //创建Excel对象
    public static void t() throws IOException {
        //创建工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
        //创建工作表对象
        HSSFSheet sheet = workbook.createSheet();
        //创建工作表的行
        HSSFRow row = sheet.createRow(0);//设置第一行,从零开始
        row.createCell(0, CellType.STRING).setCellValue("序号");
        row.createCell(1, CellType.STRING).setCellValue("编码");
        row.createCell(2, CellType.STRING).setCellValue("名称");
        row.createCell(3, CellType.STRING).setCellValue("单位");
        row.createCell(4, CellType.STRING).setCellValue("量");

        row.createCell(5, CellType.STRING).setCellValue("量详细");
        row.createCell(6, CellType.STRING).setCellValue("");
        row.createCell(7, CellType.STRING).setCellValue("");
        row.createCell(8, CellType.STRING).setCellValue("");
        row.createCell(9, CellType.STRING).setCellValue("");
        row.createCell(10, CellType.STRING).setCellValue("");
        row.createCell(11, CellType.STRING).setCellValue("");
        row.createCell(12, CellType.STRING).setCellValue("");
        row.createCell(13, CellType.STRING).setCellValue("");
        row.createCell(14, CellType.STRING).setCellValue("");
        row.createCell(15, CellType.STRING).setCellValue("");
        row.createCell(16, CellType.STRING).setCellValue("");

        row = sheet.createRow(1);//设置第一行,从零开始
        row.createCell(0, CellType.STRING).setCellValue("1");
        row.createCell(1, CellType.STRING).setCellValue("2");
        row.createCell(2, CellType.STRING).setCellValue("3");
        row.createCell(3, CellType.STRING).setCellValue("4");
        row.createCell(4, CellType.STRING).setCellValue("5");
        // 月份
        for (int i = 1; i <= 12; i ++) {
            row.createCell(i + 4, CellType.STRING).setCellValue(String.valueOf(i));
        }

        CellRangeAddress region = new CellRangeAddress(0, 1, 0, 0);
        sheet.addMergedRegion(region);

        region = new CellRangeAddress(0, 1, 1, 1);
        sheet.addMergedRegion(region);

        region = new CellRangeAddress(0, 1, 2, 2);
        sheet.addMergedRegion(region);

        region = new CellRangeAddress(0, 1, 3, 3);
        sheet.addMergedRegion(region);

        region = new CellRangeAddress(0, 1, 4, 4);
        sheet.addMergedRegion(region);

        region = new CellRangeAddress(0, 0, 5, 16);
        sheet.addMergedRegion(region);

        //文档输出
        FileOutputStream out = new FileOutputStream("F:\\tmp\\hg\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls");
        workbook.write(out);
        out.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值