(2) POI 读word表格

第一段
Java code
    
    
    
    
try { FileInputStream in = new FileInputStream( " D:\\XBRL.doc " ); // 载入文档 POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange(); // 得到文档的读取范围 int paraNum = range.numParagraphs(); int m = 0 ; // 数组下标 String[] ret = new String[paraNum]; for ( int i = 0 ; i < paraNum; ++ i) { // 从每一段落中获取文字,每一段是一个回车 Paragraph p = range.getParagraph(i); boolean test = p.isInTable(); // 判断该Paragraph是否在word的表格中 if (test == true ) { Table table = range.getTable(p); // 通过第一个在table中的Paragraph来获取整个table int numRow = table.numRows(); // 获取table中的行数 for ( int j = 0 ; j < numRow; j ++ ) { TableRow tablerow = table.getRow(j); // 获得一行 int numbercell = tablerow.numCells(); // 通过tablerow获取单元格个数 for ( int k = 0 ; k < numbercell; k ++ ) { TableCell tablexell = tablerow.getCell(k); // 获得单元格 int p1 = tablexell.numParagraphs(); // 获取单元格中的Paragraph的个数 String str = "" ; for ( int l = 0 ; l < p1; l ++ ) { Paragraph para = tablexell.getParagraph(l); str = str + para.text().trim(); // 得到单元格中的内容 } for ( int n = m; n < paraNum;) { ret[n] = str; // 将单元格中的内容方入数组元素中 break ; } m ++ ; } } } } for ( int i = 0 ; i < paraNum; i ++ ) { System.out.println(ret[i]); } } catch (Exception e) { e.printStackTrace(); }

第二段
Java code
    
    
    
    
try { POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( " D:\\XBRL.doc " )); HWPFDocument doc = new HWPFDocument(fs); Range range = doc.getOverallRange(); TableIterator ti = new TableIterator(range); // Table table = null; while (ti.hasNext()) { System.out.println( " Getting table! " ); Table table = (Table)ti.next(); System.out.println( " Number of rows: " + table.numRows()); for ( int a = 0 ; a < table.numRows(); a ++ ) { TableRow row = table.getRow(a); System.out.println( " \tTable row number: " + a); for ( int b = 0 ; b < row.numCells(); b ++ ) { System.out.println( " \t\tTable cell number: " + b); TableCell tablecell = row.getCell(b); for ( int c = 0 ; c < tablecell.numParagraphs(); c ++ ) { Paragraph tablepara = tablecell.getParagraph(c); for ( int d = 0 ; d < tablepara.numCharacterRuns(); d ++ ) { CharacterRun run = tablepara.getCharacterRun(d); System.out.println( " \t\tText: \" " + run.text() + " \" " ); } } } } System.out.println( " \n " ); } } catch (Exception e) { e.printStackTrace(); }


你可能感兴趣的:((2) POI 读word表格)