Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a part of the cell contents to Underline using Apache POI?

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

like image 916
Mustafa Avatar asked Jan 05 '14 10:01

Mustafa


People also ask

How do you make a cell value bold in Apache POI?

HSSFWorkbook hwb=new HSSFWorkbook(); HSSFSheet sheet=hwb. crateSheet("New Sheet"); HssfRow headRow=sheet. createRow((int)0); CellStyle style=headRow. getRowStyle(); Font boldFont=hwb.

How do I wrap text in Apache POI?

Text WrappingCellStyle wrap = wb. createCellStyle(); wrap. setWrapText( true );

What is XSSF and HSSF?

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.


1 Answers

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 enter image description here

You can change the font colors as well in the same way... refer here

like image 159
Sankumarsingh Avatar answered Nov 12 '22 10:11

Sankumarsingh