Java 导出Excel高亮

Java 导出Excel高亮

1 依赖

<!-- hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.34</version>
</dependency>

<!-- Apache POI 主库 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.3.0</version>
</dependency>

<!-- Apache POI 支持 Excel 2007+ (XSSF格式) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.3.0</version>
</dependency>

2 代码

package com.xu.excel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelWriter;

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.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class LightTest {

    public static void main(String[] args) {
        ExcelWriter writer = new ExcelWriter("2.xlsx");
        // 获取 Apache POI 的 Sheet 和 Workbook
        Workbook workbook = writer.getWorkbook();
        Sheet sheet = writer.getSheet();


        // 创建表头行
        Row headerRow = sheet.createRow(0);
        Cell header = headerRow.createCell(0);
        header.setCellValue("表头名称");

        // 设置表头样式
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        header.setCellStyle(style);
        sheet.setColumnWidth(0, 30 * 256);


        String light = "高亮";

        List<String> texts = new ArrayList<>();
        texts.add("Hello Hutool World");
        texts.add("测试");
        texts.add("测试某一段高亮");
        texts.add("高亮");
        texts.add("这个也需要高亮一下");
        texts.add("这个不要");
        int i = 1;
        for (String text : texts) {
            // 创建一行
            Row row = sheet.createRow(i);
            Cell cell = row.createCell(0);
            i++;
            // 创建富文本样式
            RichTextString richText = workbook.getCreationHelper().createRichTextString(text);
            // 将 "Hutool" 高亮
            if (StrUtil.contains(text, light)) {
                Font highlightFont = workbook.createFont();
                highlightFont.setBold(true);
                highlightFont.setColor(IndexedColors.YELLOW.getIndex());
                int start = text.indexOf(light);
                richText.applyFont(start, (start) + light.length(), highlightFont);
            }
            cell.setCellValue(richText);
        }


        // 写出到文件
        writer.flush();
        writer.close();
    }

}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值