在Apache-POI中读取doc文件的表格单元格中的样式名称

时间:2014-06-20 15:16:59

标签: apache-poi

我能够读取表格单元格,但我希望也能读取表格中每行单元格的应用样式名称。我怎样才能做到这一点?

修改

以下是我尝试过的代码片段。通过这个我能够读取单元格文本也应用pstyle(para style),但不能读取rstyles。

private static void processDoc(String path) throws Exception {
        POIFSFileSystem fis = new POIFSFileSystem(new FileInputStream(path));
        HWPFDocument wdDoc = new HWPFDocument(fis);
        // list all style names and indexes in stylesheet
        /*for (int j = 0; j < wdDoc.getStyleSheet().numStyles(); j++) {
            if (wdDoc.getStyleSheet().getStyleDescription(j) != null) {
                System.out.println(j + ": " + wdDoc.getStyleSheet().getStyleDescription(j).getName());
            } else {
                // getStyleDescription returned null
                System.out.println(j + ": " + null);
            }
        }*/

     // set range for entire document
        Range range = wdDoc.getRange();     
        for (int i = 0; i < range.numParagraphs(); i++) {
            Paragraph p = range.getParagraph(i);
            // check if style index is greater than total number of styles
            if (wdDoc.getStyleSheet().numStyles() > p.getStyleIndex()) {
                //System.out.println(wdDoc.getStyleSheet().numStyles() + " -> " + p.getStyleIndex());
                StyleDescription style = wdDoc.getStyleSheet().getStyleDescription(p.getStyleIndex());
                String styleName = style.getName();
                // write style name and associated text
                System.out.println(styleName + " -> " + p.text().replaceAll("[\u0000-\u001f]", ""));
            } else {
                System.out.println("\n" + wdDoc.getStyleSheet().numStyles() + " ----> " + p.getStyleIndex());
            }
        }
    }

0 个答案:

没有答案