3

私の目的は、.docx ファイルを読み取り、そのテキストをビュー (Web ページ) に表示することです。

Grails アプリケーションで .docx ファイルを読み取るために apache POI を使用しています。空白や改行を失わずにビューに出力を表示する方法を教えてください。

私の .docx ドキュメント コンテンツ

This is a .docx document ...
this is second line
this is third line

私が印刷しているときに読んだ後のGroovyコンソールの結果:

This is a .docx document ...
this is second line
this is third line

しかし、出力をビューに渡すと、

This is a .docx document ... this is second line this is third line

.

My code is : 

    import org.apache.poi.xwpf.usermodel.XWPFDocument
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor

    ...
            String str = "E:\\Query.docx"
            File docFile = null;
            docFile = new File(str);
            FileInputStream fis=new FileInputStream(docFile.getAbsolutePath());
            XWPFDocument doc = new XWPFDocument(fis)
            XWPFWordExtractor docExtractor =  new XWPFWordExtractor(doc)
            println docExtractor.getText()
    ...

ドキュメントの各行を反復する方法を提案できれば、簡単に結果を得ることができます。行き詰まってしまった私を助けてください。

4

1 に答える 1

1

HTML は改行を無視します。そのため、"Hello there\nLine 2\n" のような文字列は、コンソールでは次のように正しくレンダリングされます。

Hello There
Line 2

HTML として、すべて同じ行に表示されます。改行文字を適切な HTML に置き換える必要があります。たとえば<br />、段落/div タグでラップするなどです。

于 2012-10-08T11:40:15.030 に答える