Apache POI highlight excel text with XSSFRichTextString

Apache POI highlight excel text with XSSFRichTextString

This blog describe how to highlight excel text with XSSFRichTextString

Install Java 11

Install Intellij

open Intellij and choose File -> New -> Project...

No alt text provided for this image

Choose Maven

No alt text provided for this image

Enter name of the project and press Finish

No alt text provided for this image


Open pom.xml. Add poi and poi-ooxml dependency to pom.xml file

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>        


Now we have pom.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>java-common-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>
    </dependencies>


</project>        


Create a package, right click to it and choose New -> Java class

No alt text provided for this image

Enter XSSFRichTextStringExample as class name

No alt text provided for this image


add follow code to class


import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class XSSFRichTextStringExample {

    public static void main(String[] args) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("XSSFRichTextStringExample");
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("The Napoleonic Wars were a series of major global conflicts pitting the French Empire and its allies, led by Napoleon I");

        XSSFRichTextString richTextString = cell.getRichStringCellValue();
        XSSFFont fontRed = workbook.createFont();
        fontRed.setColor(IndexedColors.RED.getIndex());

        XSSFFont fontGreen = workbook.createFont();
        fontGreen.setColor(IndexedColors.GREEN.getIndex());

        richTextString.applyFont(1,5, fontRed);
        richTextString.applyFont(6,9, fontGreen);

        cell.setCellValue(richTextString);

        try (OutputStream fileOut = new FileOutputStream("XSSFRichTextStringExample.xlsx")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}        

Run this class. Open project folder we will see excel file with highlighted text


No alt text provided for this image



No alt text provided for this image

要查看或添加评论,请登录

Tung Vo的更多文章

  • MD5 message-digest algorithm

    MD5 message-digest algorithm

    MD5 (Message-Digest Algorithm 5) is a widely used cryptographic hash function that generates a fixed-length 128-bit…

  • Oracle UTL_SMTP send mail with TLS

    Oracle UTL_SMTP send mail with TLS

    This article discribes send email with TLS by using Oracle UTL_SMTP Transport Layer Security (TLS) WIKI is a…

  • Mahalanobis distance and classifier

    Mahalanobis distance and classifier

    This article illustrates classify the pattern using Mahalanobis distance sampe variance sampe variance Sample…

  • Oracle ADF Show detail viewObject by request URL

    Oracle ADF Show detail viewObject by request URL

    Show detail viewObject by request URL Install Java Install JDeveloper Open JDeveloper, choose New Application..

  • Oracle ADF fetch detail ViewObject by request URL

    Oracle ADF fetch detail ViewObject by request URL

    This article address how to fetch detail data of View object by request parameter Install Java Install JDeveloper Open…

    1 条评论
  • Oracle ADF Binding value dropdown to transient attributes

    Oracle ADF Binding value dropdown to transient attributes

    This article Oracle ADF Binding value dropdown to transient attributes Install Java Install JDeveloper Open Jdeveloper…

  • SOLID Principles

    SOLID Principles

    SOLID là vi?t t?t c?a 5 nguyên t?c thi?t k? h??ng ??i t??ng (OOP) giúp cho code d? ??c, d? hi?u, d? b?o trì h?n. Các…

  • Java parallelStream

    Java parallelStream

    Introduced in Java 8, parallel streams are a powerful tool for performing operations on collections in a concurrent and…

  • Java ConcurrentHashMap

    Java ConcurrentHashMap

    Sample ConcurrentHashMap The ConcurrentHashMap class in Java is a thread-safe implementation of the Map interface. This…

  • Java ExecutorService

    Java ExecutorService

    Interface ExecutorService of package java.util.

社区洞察

其他会员也浏览了