I am working on a program in which I have to set cell value in an Excel spreadsheet like
"This is an Underlined text".
It can be anything Bold, Italic, or Underline.
I am using Apache POI 3.9
HSSFWorkbook hwb=new HSSFWorkbook(); HSSFSheet sheet=hwb. crateSheet("New Sheet"); HssfRow headRow=sheet. createRow((int)0); CellStyle style=headRow. getRowStyle(); Font boldFont=hwb.
Text WrappingCellStyle wrap = wb. createCellStyle(); wrap. setWrapText( true );
HSSF (Horrible Spreadsheet Format) − It is used to read and write xls format of MS-Excel files. XSSF (XML Spreadsheet Format) − It is used for xlsx file format of MS-Excel.
Try the following:
public static void differentFontTypeInSameCell(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("TestSheet");
Cell cell = sheet.createRow(0).createCell(0);
Font underlineFont = wb.createFont();
underlineFont.setUnderline(HSSFFont.U_DOUBLE);
Font boldFont = wb.createFont();
boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
Font italicFont = wb.createFont();
italicFont.setItalic(true);
CellStyle style = wb.createCellStyle();
style.setFont(underlineFont);
cell.setCellStyle(style);
RichTextString richString = new HSSFRichTextString("Underline, Bold, Italic");
richString.applyFont(11, 15, boldFont);
richString.applyFont(17, 23, italicFont);
cell.setCellValue(richString);
}
Will look like
You can change the font colors as well in the same way... refer here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With