【问题标题】:Setting a part of the cell contents to Underline using Apache POI?使用 Apache POI 将部分单元格内容设置为下划线?
【发布时间】:2019-11-26 18:10:01
【问题描述】:

我正在开发一个程序,在该程序中我必须在 Excel 电子表格中设置单元格值,例如

“这是一个下划线文本”。

它可以是粗体、斜体或下划线。

我正在使用 Apache POI 3.9

【问题讨论】:

  • 您能否在此处发布您的尝试,说明您面临的问题在哪里?
  • 实际上我无法获得代码来执行此操作,因此要求代码执行此操作,但现在得到了。

标签: java excel apache-poi


【解决方案1】:

尝试以下方法:

public static void differentFontTypeInSameCell(){
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("TestSheet");
    Cell cell = sheet.createRow(0).createCell(0);
    Font underlineFont = wb.createFont();
    underlineFont.setUnderline(HSSFFont.U_DOUBLE);
    Font boldFont = wb.createFont();
    boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    Font italicFont = wb.createFont();
    italicFont.setItalic(true);
    CellStyle style = wb.createCellStyle();
    style.setFont(underlineFont);
    cell.setCellStyle(style);
    RichTextString richString = new HSSFRichTextString("Underline, Bold, Italic");
    richString.applyFont(11, 15, boldFont);
    richString.applyFont(17, 23, italicFont);
    cell.setCellValue(richString);
}

看起来像

你也可以用同样的方法改变字体颜色...参考here

【讨论】:

  • 非常感谢,这正是我要找的东西 :)
  • 嗨,你能告诉我如何在 JTextPane 上显示这个 RichTextString 吗?
  • 感谢有用的链接,但如何从富文本字符串中提取格式?这样我就可以将其提供给样式化的文档。
猜你喜欢
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 2019-07-19
  • 2019-12-26
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多