在POE中,单个单元格中的Apache POI文本的大小不同?

问题描述:

任何人都可以请告诉我如何在单个单元格中放置各种大小的文本。比如Tittle的大小为20,字幕的大小为16 ..在相同的字符串中使用新的行分隔符。在POE中,单个单元格中的Apache POI文本的大小不同?

您需要一个RichTextString。您还需要确保该行足够高并且该单元格包装文字:

public static void main(String[] args) throws IOException { 
    FileInputStream fis = new FileInputStream(FILE_NAME); 

    XSSFWorkbook wb = new XSSFWorkbook(fis); 
    Sheet sheet = wb.getSheetAt(0); 

    Row row = sheet.rowIterator().next(); 
    Cell cell = row.cellIterator().next(); 

    XSSFFont font1 = wb.createFont(); 
    font1.setFontHeight(20); 
    font1.setBold(true); 
    XSSFFont font2 = wb.createFont(); 
    font2.setFontHeight(16); 
    font2.setBold(false); 

    XSSFRichTextString richString = new XSSFRichTextString("Hello,\nWorld!"); 
    richString.applyFont(0, 6, font1); 
    richString.applyFont(6, 13, font2); 
    cell.setCellValue(richString); 

    CellStyle cs = wb.createCellStyle(); 
    cs.setWrapText(true); 
    cell.setCellStyle(cs); 

    row.setHeightInPoints((3*sheet.getDefaultRowHeightInPoints())); 

    FileOutputStream fos = new FileOutputStream(FILE_NAME); 
    wb.write(fos); 

    fos.close(); 
    wb.close(); 
    fis.close(); 
}