java - 使用 Apache POI 来自 Excel 的 HTML 格式化单元格值

标签 java html excel apache-poi apache-tika

我正在使用 apache POI 读取 excel 文档。至少可以说,它现在能够满足我的目的。但令我震惊的一件事是将单元格的值提取为 HTML。

我有一个单元格,用户将在其中输入一些字符串并应用一些格式(如项目符号/数字/粗体/斜体)等。

所以当我阅读它时,内容应该是 HTML 格式,而不是 POI 给出的纯字符串格式。

我几乎浏览了整个 POI API,但找不到任何人。我只想保留一个特定列的格式,而不是整个 excel。我所说的列是指在该列中输入的文本。我想要该文本作为 HTML 文本。

还探索并使用了 Apache Tika。但是据我所知,它只能让我得到文本,而不能得到文本的格式。

请有人指导我。我的选项用完了。

假设我在 Excel 中写了 My name is AngelDemon

我应该在 Java 中得到的输出是 My name is <b>Angel</b> and <i>Demon</i>

最佳答案

我已将其作为 unicode 粘贴到 xls 文件的单元格 A1 中:

<html><p>This is a test. Will this text be <b>bold</b> or <i>italic</i></p></html>

此 html 行产生此内容:

这是一个测试。这段文字是粗体还是斜体

我的代码:

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>

我认为这就是您想要的,我只是向您展示了可能性,我没有使用最佳代码实践,我只是快速编程以产生输出。

关于java - 使用 Apache POI 来自 Excel 的 HTML 格式化单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610881/

相关文章:

java - 将数据放入列表中,然后将其值插入数据库

javascript - 使用javascript重置html所需的样式

javascript - 如何在WordPress中使用上传特色图片的URL

JavaScript 不会显示代码结果

Python - 将现有工作簿的格式应用于另一个工作簿

java - Spark : replace null values in dataframe with mean of column

java - 什么时候销毁数据库连接?

c# - 遍历excel表中特定列号的所有行并对每一行值做一些处理

Excel countif 单元格中的日期大于或等于另一个单元格中的日期

java - 获取文件扩展名的可靠方法