使用Apache POI从Excel格式化HTML格式的单元格值
我正在使用Apache POI来阅读一个Excel文档。 至less可以说,现在能够达到我的目的。 但是有一点我感到震惊的是将单元格的值提取为HTML。
我有一个单元格,其中用户将input一些string,并应用一些格式(如子弹/数字/粗体/斜体)等
所以当我阅读它时,内容应该是HTML格式,而不是POI给出的纯string格式。
我几乎浏览了整个POI API,但找不到任何人。 我想保留只是一个特定列的格式,而不是整个Excel。 按列我的意思是,在该列中input的文本。 我想这个文本为HTML文本。
也探索和使用Apache Tika 。 然而,据我所知,它只能让我的文本,而不是文本的格式。
请有人指导我。 我没有select。
假设我写了我的名字是天使和恶魔在Excel中。
我在Java中得到的输出是My name is <b>Angel</b> and <i>Demon</i>
我已经把这个粘贴到xls文件的单元格A1:
<html><p>This is a test. Will this text be <b>bold</b> or <i>italic</i></p></html>
这个html行产生这个:
这是一个testing。 这个文本是粗体还是斜体?
我的代码:
public class ExcelWithHtml { // <html><p>This is a test. Will this text be <b>bold</b> or // <i>italic</i></p></html> public static void main(String[] args) throws FileNotFoundException, IOException { new ExcelWithHtml() .readFirstCellOfXSSF("/Users/rcacheira/testeHtml.xlsx"); } boolean inBold = false; boolean inItalic = false; public void readFirstCellOfXSSF(String filePathName) throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream(filePathName); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sheet = wb.getSheetAt(0); String cellHtml = getHtmlFormatedCellValueFromSheet(sheet, "A1"); System.out.println(cellHtml); fis.close(); } public String getHtmlFormatedCellValueFromSheet(XSSFSheet sheet, String cellName) { CellReference cellReference = new CellReference(cellName); XSSFRow row = sheet.getRow(cellReference.getRow()); XSSFCell cell = row.getCell(cellReference.getCol()); XSSFRichTextString cellText = cell.getRichStringCellValue(); String htmlCode = ""; // htmlCode = "<html>"; for (int i = 0; i < cellText.numFormattingRuns(); i++) { try { htmlCode += getFormatFromFont(cellText.getFontAtIndex(i)); } catch (NullPointerException ex) { } try { htmlCode += getFormatFromFont(cellText .getFontOfFormattingRun(i)); } catch (NullPointerException ex) { } int indexStart = cellText.getIndexOfFormattingRun(i); int indexEnd = indexStart + cellText.getLengthOfFormattingRun(i); htmlCode += cellText.getString().substring(indexStart, indexEnd); } if (inItalic) { htmlCode += "</i>"; inItalic = false; } if (inBold) { htmlCode += "</b>"; inBold = false; } // htmlCode += "</html>"; return htmlCode; } private String getFormatFromFont(XSSFFont font) { String formatHtmlCode = ""; if (font.getItalic() && !inItalic) { formatHtmlCode += "<i>"; inItalic = true; } else if (!font.getItalic() && inItalic) { formatHtmlCode += "</i>"; inItalic = false; } if (font.getBold() && !inBold) { formatHtmlCode += "<b>"; inBold = true; } else if (!font.getBold() && inBold) { formatHtmlCode += "</b>"; inBold = false; } return formatHtmlCode; } }
我的输出:
This is a test. Will this text be <b>bold</b> or <i>italic</i>
我认为这是你想要的,我只是告诉你的可能性,我没有使用最好的代码做法,我只是快速编程产生输出。
- 如何格式化由POI创build的Excel文档
- 需要一个不使用公式的apache POI createDateConstraint的工作示例
- org.apache.poi.POIXMLException:使用Libre Office更新.xls文件后POI API中的java.lang.reflect.InvocationTargetException
- 如何让用户从Java / Struts的Web应用程序中将数据库中的数据下载到Excel表单文件中?
- Apache POI在SXSSF工作簿中评估公式
- 无法从文本单元格获取数字值
- 打印Excel文件的单元格值
- 在java中使用jxls编写大量的行
- setCellValue不起作用