Change Cell Font Name - Introduction
In this Excel formatting options series, we will provide example code to change the cell font name. i.e. Arial, Verdana , using Apache POI in Java. We have separate examples to handle XLS and XLSX formats, and you can use the one that you need depending on your requirements. Let us get started with the tutorials.
XLS Format - Change Font Face for Cell - Java Program Example
The Java program that you can use to change font type for a cell to a different font name is provided below. You use the setFontName method in HSSFFont class, and pass the font name to the method. This sets a specific font that you can attach to a style and then to any cell you want. Here is the program that sets the font name to Verdana.
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class ChangeCellFontName {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
/* Get access to HSSFCellStyle */
HSSFCellStyle my_style = my_workbook.createCellStyle();
/* Create HSSFFont object from the workbook */
HSSFFont my_font=my_workbook.createFont();
/* Set the font name to Verdana */
my_font.setFontName("Verdana");
/* Also make the font color to RED */
my_font.setColor(HSSFFont.COLOR_RED);
/* attach the font to the style created earlier */
my_style.setFont(my_font);
/* Attach the new font to a cell */
/* Create a row in the sheet */
Row row = my_sheet.createRow(0);
/* Create a cell */
Cell cell = row.createCell(0);
cell.setCellValue("The font for this text would be Verdana");
/* Attach the style to the cell */
cell.setCellStyle(my_style);
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:\\verdana_font_name.xls"));
my_workbook.write(out);
out.close();
}
}
Change Font Name - XLSX Example in POI
You can use the same method (i.e. setFontName) to change the font name in XLSX worksheets also. Here is a XLSX example program to change font name to Arial.
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
public class ChangeCellFontNameXLSX {
public static void main(String[] args) throws Exception{
/* Create Workbook and Worksheet XLSX Format */
XSSFWorkbook my_workbook = new XSSFWorkbook();
XSSFSheet my_sheet = my_workbook.createSheet("Cell Font");
/* Get access to XSSFCellStyle */
XSSFCellStyle my_style = my_workbook.createCellStyle();
/* Create XSSFFont object from the workbook */
XSSFFont my_font=my_workbook.createFont();
/* Set font name to Arial */
my_font.setFontName("Arial");
/* Also make the font color to RED */
my_font.setColor(XSSFFont.COLOR_RED);
/* attach the font to the style created earlier */
my_style.setFont(my_font);
/* Create a Cell and attach the special font */
/* Create a row in the sheet */
Row row = my_sheet.createRow(0);
/* Create a cell */
Cell cell = row.createCell(0);
cell.setCellValue("The font name for this text would be Arial");
/* Attach the style to the cell */
cell.setCellStyle(my_style);
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:\\Arial_Font_Example.xlsx"));
my_workbook.write(out);
out.close();
}
}
setFontName - Output Example
A sample output for one of the programs above is given below. (Note: you need to get the right JAR files depending on the example you pick)
Change Cell Font Name - setFontName- Java POI Example |
No comments:
Post a Comment