Get the App
SLTechnology News&Howtos  ›  Development  › 

How Java applies one or more font styles to Excel cells

Shulou Source: shulou.com Published: 2022-06-02 12:55:14 10月01日 Update

This article mainly introduces Java in Excel cells how to apply one/more font styles, the text is very detailed, has a certain reference value, interested friends must read!

In Excel, when setting cell font style, you can apply the same style to all characters in the cell, that is, obtain the specified cell and apply the style; in addition, you can apply different font styles to different character contents in the cell, that is, obtain the character position in the cell and apply the style;

Tools: Free Spire.XLS for Java (Free Edition)

Note: You can download the package through the official website, extract it, and import the Spire.Xls.jar file in the lib folder into Java programs; or import it through the maven repository. The import effect is as follows:

Java code examples

import com.spire.xls.*; import java.awt.*; public class SetMultipleStylesInOneCell { public static void main(String[] args) { //load Excel document Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //Get the specified worksheet Worksheet sheet = wb.getWorksheets().get(0); //Get cell C3, apply style to all characters in cell CellRange cell1 = sheet.getCellRange("C3"); cell1.getCellStyle().getFont().setFontName("line script"); cell1.getCellStyle().getFont().setColor(new Color(0,128,0)); cell1.getCellStyle().getFont().setUnderline(FontUnderlineType.Double); cell1.getCellStyle().getFont().setSize(13); //Get cell C5 CellRange cell2 = sheet.getCellRange("C5"); //Create font styles font1 and font2 ExcelFont font1 = wb.createFont(); font1.setColor(new Color(30,144,255)); font1.setFontName("bold"); font1.setSize(14); font1.isItalic(true); ExcelFont font2 = wb.createFont(); font2.setColor(new Color(220,20,60)); font2.setFontName("italics"); font2.setSize(17); font2.isBold(true); //call the method to set font style to the specified character in the cell RichText richText = cell2.getRichText(); richText.setFont(0,7,font1); richText.setFont(9,17,font2); //Save document wb.saveToFile("Result.xlsx"); wb.dispose(); } }

Font style application results:

The above is "Java in Excel cells how to apply one/more font style" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Units styles applications fonts characters content multiple different files documents articles boxes warehouses code values locations interests guys buddies tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Redmi Shulou Tech Info Xiaomi