导入 Jar 依赖
方法一:下载免费的 Spire.XLS for Java并解压,然后将 Spire.Xls.jar 文件作为依赖添加到您的项目中。
方法二:在pom.xml中添加如下配置,直接将jar依赖添加到maven项目中。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
示例代码
import com.spire.xls.*;
import java.awt.*;
public class ApplyMultiFontsInCell {
public static void main(String[] args) {
//Create a Workbook instance
Workbook wb = new Workbook();
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Create first Excel font
ExcelFont font1 = wb.createFont();
font1.setFontName("Calibri");
font1.setColor(Color.blue);
font1.setSize(12f);
font1.isItalic(true);
//Create second Excel font
ExcelFont font2 = wb.createFont();
font2.setFontName("Times New Roman");
font2.setColor(Color.BLACK);
font2.setSize(12f);
font2.isItalic(true);
font2.setUnderline(FontUnderlineType.Double);
//Create third Excel font
ExcelFont font3 = wb.createFont();
font3.setFontName("Arial");
font3.setColor(Color.RED);
font3.setSize(14f);
font3.isBold(true);
//Insert text to cell B5
RichText richText = sheet.getCellRange("B5").getRichText();
richText.setText("This is an example of how to apply multiple font styles in one Excel cell using Java.");
//Apply three fonts to the text in the cell B5
richText.setFont(0, 21, font1);
richText.setFont(22, 55, font2);
richText.setFont(56, 84, font3);
//Save the document
wb.saveToFile("MultiFonts.xlsx", ExcelVersion.Version2016);
}
}