Apache POI 3.15老版本,合并单元格

1 篇文章 0 订阅
1 篇文章 0 订阅

POI3.15 老版本 合并单元格时,合并后单元格内容为合并前左上角第一个单元格的文本内容,
可用\n换行,但是要在Style中设置 style.setWrapText(true);
每个单元格的样式要单独设置, 合并后的大单元格会自动去掉中间的边框,只剩下四周边框

package com.dxk.controller;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

public class ExcelExample {
    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("sheet1");

        Row row1 = sheet.createRow(0);
        Row row2 = sheet.createRow(1);
        Row row3 = sheet.createRow(2);
        for (int i = 0; i < 10; i++) {
            Cell cell1 = row1.createCell(i);
            cell1.setCellValue("cell1" + i);
            Cell cell2 = row2.createCell(i);
            cell2.setCellValue("cell2" + i);
            Cell cell3 = row3.createCell(i);
            cell3.setCellValue("cell3" + i);
        }
        
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short)10);
        CellStyle style = wb.createCellStyle();
//        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 填充样式为前景色
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); // 前景色为黄色
        style.setBorderBottom(BorderStyle.THIN); // 下边框线条为实线
        style.setBorderTop(BorderStyle.THIN); // 上边框线条为实线
        style.setBorderLeft(BorderStyle.THIN); // 左边框线条为实线
        style.setBorderRight(BorderStyle.THIN); // 右边框线条为实线
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框线条颜色为黑色
        style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框线条颜色为黑色
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框线条颜色为黑色
        style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框线条颜色为黑色
        style.setWrapText(true);  // 设置单元格文本自动换行

        Cell cell1 = sheet.getRow(1).getCell(0);
        cell1.setCellStyle(style);
        cell1.setCellValue("部门\n名称\n哈哈哈哈哈哈哈");
        Cell cell2 = sheet.getRow(2).getCell(0);
        cell2.setCellStyle(style);
        cell1.setCellValue("111111");

        sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
        sheet.autoSizeColumn(0); // 自适应列宽

        FileOutputStream fos = new FileOutputStream("D://output.xlsx");
        wb.write(fos);
        fos.close();
        wb.close();
        System.out.println("完成");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache poi 导出excel时合并单元可以使用setRegionStyle方法来设置合并单元的样式。这个方法接受三个参数,分别是Sheet对象、CellRangeAddress对象和CellStyle对象。在这个方法中,通过循环遍历合并单元的每一行和每一列,然后获取对应的单元并设置样式。具体的代码实现可以参考引用\[1\]中的示例代码。 此外,还可以使用PoiModel类来定义导出excel时的数据模型。这个类包含了内容、上一行同一位置内容、行标、列标等属性。可以根据实际需求来使用这个类。具体的代码实现可以参考引用\[2\]和引用\[3\]中的示例代码。 总结起来,Apache poi 导出excel时合并单元的步骤包括设置合并单元的样式和定义导出数据的模型。可以根据具体的需求来使用相应的方法和类来实现导出功能。 #### 引用[.reference_title] - *1* *3* [poi 导出Excel 动态 合并单元](https://blog.csdn.net/weixin_65436454/article/details/127806178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用POI 导出Excel 动态合并单元](https://blog.csdn.net/weixin_41722928/article/details/112849624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值