XSSFWorkbook多级表头代码实现

该代码示例展示了如何使用ApachePOI库在Java中创建一个新的Excel工作簿,添加数据行,合并单元格,设置样式,并保存文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package DEMO;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


public class ExcelDemo2 {
    public static void main(String[] args) throws IOException {

        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("Data Sheet");

        Row row1 = sheet.createRow(0);
        Cell cell11 = row1.createCell(0);

        cell11.setCellValue("填报单位");
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));

        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("住院分娩活产数");
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 9));

        //创建第二行
        Row row2 = sheet.createRow(1);

        Cell cell21 = row2.createCell(1);
        cell21.setCellValue("分性别活产数");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 4));
        Cell cell22 = row2.createCell(5);
        cell22.setCellValue("分孕周活产数");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 5, 8));
        Cell cell23 = row2.createCell(9);
        cell23.setCellValue("<28周存货儿数");
        sheet.addMergedRegion(new CellRangeAddress(1, 2, 9, 9));

        //创建第二行
        Row row3 = sheet.createRow(2);
        Cell cell31 = row3.createCell(1);
        cell31.setCellValue("男");
        Cell cell32 = row3.createCell(2);
        cell32.setCellValue("女");
        Cell cell33 = row3.createCell(3);
        cell33.setCellValue("性别不明");
        Cell cell34 = row3.createCell(4);
        cell34.setCellValue("合计");
        Cell cell35 = row3.createCell(5);
        cell35.setCellValue("≥37周");
        Cell cell36 = row3.createCell(6);
        cell36.setCellValue("28~36周");
        Cell cell37 = row3.createCell(7);
        cell37.setCellValue("合计");
        Cell cell38 = row3.createCell(8);
        cell38.setCellValue("早产率%");

        CellStyle style = wb.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);

        int columSum = 10;
        int rowCount = 100;

        for (int i = 3; i < rowCount + 3; i++) {
            Row row = sheet.createRow(i);
            for (int j = 0; j < columSum; j++) {
                Cell c = row.createCell(j);
                c.setCellValue(i+"---"+j);
            }
        }

        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell == null || cell.getCellType() == CellType.BLANK) { // 判断cell是否为空
                    continue;
                }
                cell.setCellStyle(style);
            }
        }
        List<CellRangeAddress> mergedCellsPosition = sheet.getMergedRegions();
        for (CellRangeAddress cellRangeAddress : mergedCellsPosition) {
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);


        }
        try (FileOutputStream fileOut = new FileOutputStream("c9.xlsx")) {
            wb.write(fileOut);
        }
        wb.close();
    }
}

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.1.0</version>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值