具有混合字体的XSSFRichTextString仅在加粗字体之后忽略换行符

时间:2018-07-31 10:07:10

标签: java apache-poi xssf

在下面的代码中,我打算将“ Jack Reacher”以粗体显示,并将其余字符串作为普通字体显示。我看不到“ Jack Reacher”后面的换行符。如果我评论并忽略粗体descEntryRichStr.applyFont(0, name.length() , boldedFont);

,则会应用换行符

我也添加了cs.setWrapText(true);,但仍然不起作用

XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Test First");

    XSSFFont boldedFont = workbook.createFont();
    boldedFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

    String newLine  = "\n";
    String name = "Jack Reacher";
    String fileNo = "Professsion : Doctor";
    String other = "Address : New-york" ;

    String finalString = name +newLine + fileNo + newLine + other;

    Row row = sheet.createRow(0); 
    XSSFCell cell = (XSSFCell) row.createCell(0);

    CreationHelper creationHelper = workbook.getCreationHelper();

    XSSFRichTextString descEntryRichStr = (XSSFRichTextString) creationHelper.createRichTextString(finalString);
    descEntryRichStr.applyFont(0, name.length() , boldedFont);
    cell.setCellValue(descEntryRichStr);
    XSSFCellStyle cs = cell.getCellStyle();
    cs.setWrapText(true);
    cell.setCellStyle(cs);

    System.out.println(cell.getStringCellValue());

    try (FileOutputStream outputStream = new FileOutputStream("/shared/sample/report-"+new Date().getTime() +".xlsx")) {
        workbook.write(outputStream);
    }

Here is the snippet of report

0 个答案:

没有答案