用于Libre电子表格的RTF到HTML - Java

时间:2014-02-20 03:51:58

标签: java html rtf libreoffice jeditorpane

我正在尝试使用Libre将RTF转换为HTML。它适用于Libre Document,但为Spreadsheet提供null值。下面是我的java代码,我是如何解析的。

ArrayList<String> styleValue = new ArrayList<String>();
String result;
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;
result = rtfToHtml(new StringReader(streamToString((InputStream) contents.getTransferData(dfRTF))));

rtfToHtml()

public static String rtfToHtml(Reader rtf) throws IOException { 
        JEditorPane p = new JEditorPane();
        p.setContentType("text/rtf");
        EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
        try {
            kitRtf.read(rtf, p.getDocument(), 0);
            kitRtf = null;
            EditorKit kitHtml = p.getEditorKitForContentType("text/html");
            Writer writer = new StringWriter();
            kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
            return writer.toString();

        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        return null;
    }

赞赏是否有人可以帮我修复Libre电子表格。请指教。

0 个答案:

没有答案