在下面的代码中,我打算将“ 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);
}