Bijoy Bahuleyan
Bijoy Bahuleyan

Reputation: 91

How to get the font style of a cell field of excel in Java, Apache POI?

I would like to capture the font of a cell field in excel in Java. I am using Apache POI. If possible, I would like to capture font-color, font-family, font-weight, font-size, etc.

How can I achieve this?

Upvotes: 4

Views: 11882

Answers (2)

PeterKe
PeterKe

Reputation: 1

I have it in Groovy but the principle should be similar:

import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.ss.util.*
import java.io.*

InputStream file = new FileInputStream('C:\\Folder\\File.xls')
Workbook wb = WorkbookFactory.create(file)
Sheet sheet1 = wb.getSheet('Sheet1')
// to get first row
Row row = sheet1.getRow(0)
// to get first cell
Cell cell = row.getCell(0)
// to get its style
HSSFCellStyle cellStyle = cell.getCellStyle()
// to get font
HSSFFont cellFont = cellStyle.getFont(wb)
// to get font name as text
String cellFontName = cellFont.getFontName()

Upvotes: 0

ManishChristian
ManishChristian

Reputation: 3784

Edited based on comment

You can refer to XSSFCellStyle, from it you can get the XSSFFont. Using that you can get XSSFColor, getFontName() or getFamily() and getFontHeight() or getFontHeightInPoints().

Based on example cell that I've used:

XSSFCellStyle cs = cell.getCellStyle();
XSSFFont font = cs.getFont();

//Getting Font color
XSSFColor color = font.getXSSFColor();
System.out.println("Font color : " + color.getARGBHex());
//==>   FF00B0F0

//Getting Font name
System.out.println("Font name : " + font.getFontName());
//==>   Arial

//Getting Font family name
FontFamily family = FontFamily.valueOf(((XSSFFont) font).getFamily());
System.out.println("Font family : " + family);
//==>   SWISS

//Getting Font family int
System.out.println("Font family in int : " + font.getFamily());
//==>   2

//Getting Font height
System.out.println("Font FontHeight : " + font.getFontHeight());
//==>   280

//Getting Font height in point
System.out.println("Font height in point : " + font.getFontHeightInPoints());
//==>   14

//Getting Font bold weight  
System.out.println("Font BoldWeight : " + font.getBoldweight());
//==>   700

Upvotes: 8

Related Questions