2017-06-05 141 views

回答

1

您所想,RichTextString應該能夠解釋HTML?不是這樣。它只能從startIndexendIndexapply Font。並且可以使用TypeOffsetSS_SUPER來標記Font

例子:

import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*; 

import org.apache.poi.ss.util.*; 

import java.io.FileOutputStream; 

class RichTextSuperscript { 

public static void main(String[] args) { 
    Workbook wb = new XSSFWorkbook(); 
    Sheet sheet = wb.createSheet("Sheet1"); 

    Font fontRed = wb.createFont(); 
    fontRed.setColor(Font.COLOR_RED); 

    Font fontRedSuperscript = wb.createFont(); 
    fontRedSuperscript.setColor(Font.COLOR_RED); 
    fontRedSuperscript.setTypeOffset(Font.SS_SUPER); 

    String string = "Level 3"; 

    RichTextString richString = new XSSFRichTextString("Level 13"); 
                //^0  ^7 
    richString.applyFont(7, 8, fontRedSuperscript); 

    for (int r = 0; r < 10; r++) { 
    Row row = sheet.createRow(r); 
    Cell cell = row.createCell(0); 
    if ((r%2) == 0) { 
    cell.setCellValue(string); 
    } else { 
    CellUtil.setFont(cell, fontRed); 
    cell.setCellValue(richString); 
    } 
    } 

    try { 
    wb.write(new FileOutputStream("RichTextSuperscript.xlsx")); 
    wb.close(); 
    } catch (Exception ex) { 
    ex.printStackTrace(); 
    } 
} 
} 
+0

感謝您的幫助。它完美的作品 –