Java操作Excel合并单元格操作

时间:2025-04-11 08:10:34
import .*;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
 * @author 
 * @version 1.0
 * @description
 * @date 2020/9/15 9:32
 */
public class MergeCellDemo {
    public static void main(String[] args) throws IOException {
        //新建工作簿
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //新建工作表
        XSSFSheet sheet = ("工作表1");
        //指定合并开始行、合并结束行 合并开始列、合并结束列
        CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, 1);
        //添加要合并地址到表格
        (rangeAddress);
        //创建行,指定起始行号,从0开始
        XSSFRow row = (0);
        //创建单元格,指定起始列号,从0开始
        XSSFCell cell = (0);
        //设置单元格内容
        ("我的合并后单元格");
        //创建样式对象
        CellStyle style = ();
        //设置样式对齐方式:水平垂直居中
        ();
        ();
        //设定填充单色
        (FillPatternType.SOLID_FOREGROUND);
        //设定背景颜色
        (());
        //为指定单元格设定样式
        (style);
        FileOutputStream fileOutputStream = new FileOutputStream("d:\");
        (fileOutputStream);
        ();
    }
}