【问题标题】:Display formatted Cell value of Excel sheet on JTextPane [closed]在 JTextPane 上显示 Excel 工作表的格式化单元格值 [关闭]
【发布时间】:2014-01-23 17:07:42
【问题描述】:

我有一个 Excel 表格,其中包含像

这样的单元格值

"这是一个粗体文本"。

我想在 JTextPane 上显示它。我想我与 JTextPane 的 RichTextString 和 StyledDocument 有关系,但我不确定,请帮助我。

【问题讨论】:

  • 解决方案有效还是有任何进一步的并发症?
  • 是的,它确实有效,但是当我想将 jTextPane 文本保存到 excel 文件时,我遇到了麻烦。因为它提供了html代码。有什么解决办法吗?

标签: java swing apache-poi jtextpane netbeans-7.3


【解决方案1】:

您的选择是将 excel 中检索到的单元格值作为html 写入JTextPane

我之前曾参考this 示例从 excel 中提取单元格值的 html 格式 - 每次都像魅力一样工作。

我已扩展代码以包含 JTextPane 选项以显示您的单元格内容。

基本代码:

static boolean boldsie = false;

    public static void main(String... args) {
        File excel = new File("\\test.xlsx");
        FileInputStream fis;
        try {
            fis = new FileInputStream(excel);
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet ws = wb.getSheet("Sheet1");

            int rowNum = ws.getLastRowNum() + 1;
            int colNum = ws.getRow(0).getLastCellNum();
            String[][] data = new String[rowNum][colNum];

            for (int i = 0; i < rowNum; i++) {
                XSSFRow row = ws.getRow(i);
                for (int j = 0; j < colNum; j++) {
                    XSSFCell cell = row.getCell(j);
                    String value = cell.toString();
                    data[i][j] = value;

                    showDataInTextPane(ws, cell.getReference());

                }
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void showDataInTextPane(XSSFSheet ws, String reference) {
        JFrame frame = new JFrame("Formatted POI Sample");
        Container content = frame.getContentPane();
        JPanel panel = new JPanel();

        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");

        textPane.setText(getHtmlFromExcel(ws, reference));

        panel.setLayout(new BorderLayout());
        panel.add(textPane);

        content.add(panel);
        frame.setSize(100, 100);
        frame.setVisible(true);
    }

    public static String getHtmlFromExcel(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 = "";

        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) {

            }

            htmlCode += cellText.getString().substring(
                    cellText.getIndexOfFormattingRun(i),
                    cellText.getIndexOfFormattingRun(i)
                            + cellText.getLengthOfFormattingRun(i));
        }

        if (boldsie) {
            htmlCode += "</b>";
            boldsie = false;
        }

        return htmlCode;
    }

    private static String getFormatFromFont(XSSFFont font) {
        String formatHtmlCode = "";
        if (font.getBold() && !boldsie) {
            formatHtmlCode += "<b>";
            boldsie = true;
        } else if (!font.getBold() && boldsie) {
            formatHtmlCode += "</b>";
            boldsie = false;
        }

        return formatHtmlCode;
    }

输出:

您可以根据需要进行扩展,使其更通用、符合最佳实践并包括其他格式,如斜体等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多