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();
}
}
}
java 导出excel 文本中带有上标(次方/幂)
最新推荐文章于 2024-07-22 15:23:25 发布