1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package com.diagbot.api;
- import org.apache.poi.poifs.filesystem.POIFSFileSystem;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFTable;
- import org.apache.poi.xwpf.usermodel.XWPFTableCell;
- import org.apache.poi.xwpf.usermodel.XWPFTableRow;
- import java.io.FileInputStream;
- import java.util.Iterator;
- import java.util.List;
- public class ImportWord {
- public static void main(String[] args) {
- String filepath = "E:\\危重病人APACHE II评分表量表.docx";
- parseWord(filepath);
- }
- /**
- * 读取文档中表格
- * @param filePath
- */
- public static void parseWord(String filePath){
- try{
- FileInputStream in = new FileInputStream(filePath);//载入文档
- // 处理docx格式 即office2007以后版本
- if(filePath.toLowerCase().endsWith("docx")){
- //word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
- XWPFDocument xwpf = new XWPFDocument(in);//得到word文档的信息
- Iterator<XWPFTable> it = xwpf.getTablesIterator();//得到word中的表格
- // 设置需要读取的表格 set是设置需要读取的第几个表格,total是文件中表格的总数
- int set = 1, total = 4;
- int num = set;
- // 过滤前面不需要的表格
- // for (int i = 0; i < set-1; i++) {
- // it.hasNext();
- // it.next();
- // }
- while(it.hasNext()){
- XWPFTable table = it.next();
- System.out.println("这是第" + num + "个表的数据");
- List<XWPFTableRow> rows = table.getRows();
- //读取每一行数据
- for (int i = 0; i < rows.size(); i++) {
- XWPFTableRow row = rows.get(i);
- //读取每一列数据
- List<XWPFTableCell> cells = row.getTableCells();
- for (int j = 0; j < cells.size(); j++) {
- XWPFTableCell cell = cells.get(j);
- //输出当前的单元格的数据
- System.out.print(cell.getText() + "\t");
- }
- System.out.println();
- }
- // 过滤多余的表格
- while (num < total) {
- it.hasNext();
- it.next();
- num += 1;
- }
- }
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
|