0
我想在Excel单元格中显示一些带有上标的文本。我试图将HTML字符串传递给XSSFRichTextString,但它显示HTML字符串,因为它在输出文件中。如何使用Apache POI添加上标文本
我使用Apache POI 3.16
Screenshot to explain the requirement
我想在Excel单元格中显示一些带有上标的文本。我试图将HTML字符串传递给XSSFRichTextString,但它显示HTML字符串,因为它在输出文件中。如何使用Apache POI添加上标文本
我使用Apache POI 3.16
Screenshot to explain the requirement
您所想,RichTextString应该能够解释HTML?不是这样。它只能从startIndex
到endIndex
apply 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();
}
}
}
感谢您的帮助。它完美的作品 –