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...
Choose Maven
Enter name of the project and press Finish
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
Enter XSSFRichTextStringExample as class name
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