使用Apache POI将单元格内容的一部分设置为下划线?

时间:2021-03-15 20:24:02

I am working on a program in which I have to set cell value in an Excel spreadsheet like

我正在开发一个程序,我必须在Excel电子表格中设置单元格值

"This is an Underlined text".

“这是带下划线的文字”。

It can be anything Bold, Italic, or Underline.

它可以是粗体,斜体或下划线。

I am using Apache POI 3.9

我正在使用Apache POI 3.9

1 个解决方案

#1


8  

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 使用Apache POI将单元格内容的一部分设置为下划线?

看起来像

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

您也可以以相同的方式更改字体颜色...请参阅此处

#1


8  

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 使用Apache POI将单元格内容的一部分设置为下划线?

看起来像

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

您也可以以相同的方式更改字体颜色...请参阅此处