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

Mus*_*afa 7 java excel apache-poi

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

"这是带下划线的文字".

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

我正在使用Apache POI 3.9

San*_*ngh 11

请尝试以下方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

看起来像 在此输入图像描述

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