下面是一个示例代码,展示如何使用 Apache POI 实现复合表头:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelUtil {

    public static void main(String[] args) throws IOException {
        Workbook workbook = WorkbookFactory.create(true); // 创建工作簿
        Sheet sheet = workbook.createSheet(); // 创建工作表

        // 第一行,第一列至第三列合并,设置居中、背景色、字体样式等
        Row row1 = sheet.createRow(0);
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("姓名");
        CellStyle style11 = workbook.createCellStyle();
        style11.setAlignment(CellStyle.ALIGN_CENTER);
        style11.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        style11.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style11.setFont(FontUtil.boldFont(workbook));
        cell11.setCellStyle(style11);
        CellRangeAddress region11 = new CellRangeAddress(0, 0, 0, 2);
        sheet.addMergedRegion(region11);

        // 第一行,第四列至第六列合并,设置居中、背景色、字体样式等
        Cell cell14 = row1.createCell(3);
        cell14.setCellValue("成绩");
        CellStyle style14 = workbook.createCellStyle();
        style14.setAlignment(CellStyle.ALIGN_CENTER);
        style14.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style14.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style14.setFont(FontUtil.boldFont(workbook));
        cell14.setCellStyle(style14);
        CellRangeAddress region14 = new CellRangeAddress(0, 0, 3, 5);
        sheet.addMergedRegion(region14);

        // 第二行,第一列至第二列合并,设置居中、背景色、字体样式等
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("科目");
        CellStyle style21 = workbook.createCellStyle();
        style21.setAlignment(CellStyle.ALIGN_CENTER);
        style21.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        style21.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style21.setFont(FontUtil.boldFont(workbook));
        cell21.setCellStyle(style21);
        CellRangeAddress region21 = new CellRangeAddress(1, 2, 0, 1);
        sheet.addMergedRegion(region21);

        // 第二行,第三列,设置居中、背景色、字体样式等
        Cell cell23 = row2.createCell(2);
        cell23.setCellValue("语文");
        CellStyle style23 = workbook.createCellStyle();
        style23.setAlignment(CellStyle.ALIGN_CENTER);
        style23.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        style23.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style23.setFont(FontUtil.boldFont(workbook));
        cell23.setCellStyle(style23);

        // 第二行,第四列至第五列合并,设置居中、背景色、字体样式等
        Cell cell24 = row2.createCell(3);
        cell24.setCellValue("期末成绩");
        CellStyle style24 = workbook.createCellStyle();
        style24.setAlignment(CellStyle.ALIGN_CENTER);
        style24.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style24.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style24.setFont(FontUtil.boldFont(workbook));
        cell24.setCellStyle(style24);
        CellRangeAddress region24 = new CellRangeAddress(1, 1, 3, 4);
        sheet.addMergedRegion(region24);

        // 第二行,第六列,设置居中、背景色、字体样式等
        Cell cell26 = row2.createCell(5);
        cell26.setCellValue("总分");
        CellStyle style26 = workbook.createCellStyle();
        style26.setAlignment(CellStyle.ALIGN_CENTER);
        style26.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style26.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style26.setFont(FontUtil.boldFont(workbook));
        cell26.setCellStyle(style26);

        // 第三行,第三列,设置居中、背景色、字体样式等
        Row row3 = sheet.createRow(2);
        Cell cell33 = row3.createCell(2);
        cell33.setCellValue("数学");
        CellStyle style33 = workbook.createCellStyle();
        style33.setAlignment(CellStyle.ALIGN_CENTER);
        style33.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        style33.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style33.setFont(FontUtil.boldFont(workbook));
        cell33.setCellStyle(style33);

        // 第三行,第四列,设置居中、背景色、字体样式等
        Cell cell34 = row3.createCell(3);
        cell34.setCellValue("平时成绩");
        CellStyle style34 = workbook.createCellStyle();
        style34.setAlignment(CellStyle.ALIGN_CENTER);
        style34.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style34.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style34.setFont(FontUtil.boldFont(workbook));
        cell34.setCellStyle(style34);

        // 第三行,第五列,设置居中、背景色、字体样式等
        Cell cell35 = row3.createCell(4);
        cell35.setCellValue("期中成绩");
        CellStyle style35 = workbook.createCellStyle();
        style35.setAlignment(CellStyle.ALIGN_CENTER);
        style35.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style35.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style35.setFont(FontUtil.boldFont(workbook));
        cell35.setCellStyle(style35);

        // 第三行,第六列,设置居中、背景色、字体样式等
        Cell cell36 = row3.createCell(5);
        cell36.setCellValue("100");
        CellStyle style36 = workbook.createCellStyle();
        style36.setAlignment(CellStyle.ALIGN_CENTER);
        style36.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style36.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style36.setFont(FontUtil.boldFont(workbook));
        cell36.setCellStyle(style36);

        // 填充数据
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("张三", 80, 90, 85));
        students.add(new Student("李四", 85, 88, 87));
        students.add(new Student("王五", 90, 92, 91));
        students.add(new Student("赵六", 95, 96, 97));
        for (int i = 0; i < students.size(); i++) {
            Row row = sheet.createRow(i + 3);
            Cell cell1 = row.createCell(0);
            cell1.setCellValue(students.get(i).getName());
            CellStyle style1 = workbook.createCellStyle();
            style1.setAlignment(CellStyle.ALIGN_CENTER);
            cell1.setCellStyle(style1);
            Cell cell2 = row.createCell(1);
            cell2.setCellValue("语文");
            CellStyle style2 = workbook.createCellStyle();
            style2.setAlignment(CellStyle.ALIGN_CENTER);
            cell2.setCellStyle(style2);
            Cell cell3 = row.createCell(2);
            cell3.setCellValue(students.get(i).getChinese());
            CellStyle style3 = workbook.createCellStyle();
            style3.setAlignment(CellStyle.ALIGN_CENTER);
            cell3.setCellStyle(style3);
            Cell cell4 = row.createCell(3);
            cell4.setCellValue(students.get(i).getMath1());
            CellStyle style4 = workbook.createCellStyle();
            style4.setAlignment(CellStyle.ALIGN_CENTER);
            cell4.setCellStyle(style4);
            Cell cell5 = row.createCell(4);
            cell5.setCellValue(students.get(i).getMath2());
            CellStyle style5 = workbook.createCellStyle();
            style5.setAlignment(CellStyle.ALIGN_CENTER);
            cell5.setCellStyle(style5);
            Cell cell6 = row.createCell(5);
            cell6.setCellValue(students.get(i).getTotal());
            CellStyle style6 = workbook.createCellStyle();
            style6.setAlignment(CellStyle.ALIGN_CENTER);
            cell6.setCellStyle(style6);
        }

        // 自动调整列宽
        for (int i = 0; i < 6; i++) {
            sheet.autoSizeColumn(i);
        }

        // 输出文件
        FileOutputStream outputStream = new FileOutputStream("example.xlsx");
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }

    static class Student {
        private String name;
        private int chinese;
        private int math1;
        private int math2;

        public Student(String name, int chinese, int math1, int math2) {
            this.name = name;
            this.chinese = chinese;
            this.math1 = math1;
            this.math2 = math2;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getChinese() {
            return chinese;
        }

        public void setChinese(int chinese) {
            this.chinese = chinese;
        }

        public int getMath1() {
            return math1;
        }

        public void setMath1(int math1) {
            this.math1 = math1;
        }

        public int getMath2() {
            return math2;
        }

        public void setMath2(int math2) {
            this.math2 = math2;
        }

        public int getTotal() {
            return chinese + math1 + math2;
        }
    }

    static class FontUtil {
        public static org.apache.poi.ss.usermodel.Font boldFont(Workbook workbook) {
            org.apache.poi.ss.usermodel.Font font = workbook.createFont();
            font.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD);
            return font;
        }
    }
}

这个示例代码会生成一个包含复合表头的 Excel 文件,其中包含了一个班级成绩表格,包括姓名、语文成绩、数学平时成绩、数学期中成绩和总分等信息。这个表格的复合表头包括了“姓名”和“成绩”两个大的单元格,以及“科目”、“语文”和“数学”三个单元格。

java poi 复合表头实现示例

原文地址: https://www.cveoy.top/t/topic/w5n 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录