POI3.8读取word文档的表格数据!

package cn.ccb.boup.util.poi;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 
 * @author guojigang
 * 
 */
public class OperateWord {
	/**
	 * 读取word文档的表格数据
	 * 
	 * @param filePath
	 * @return List
	 */
	public static List readWordCell(String filePath) {
		FileInputStream in = null;
		POIFSFileSystem pfs = null;
		List<String> list = new ArrayList<String>();
		try {
			in = new FileInputStream(filePath);// 载入文档
			pfs = new POIFSFileSystem(in);
			HWPFDocument hwpf = new HWPFDocument(pfs);
			Range range = hwpf.getRange();// 得到文档的读取范围
			TableIterator it = new TableIterator(range);
			// 迭代文档中的表格
			if (it.hasNext()) {
				TableRow tr = null;
				TableCell td = null;
				Paragraph para = null;
				String lineString;
				String cellString;
				Table tb = (Table) it.next();
				// 迭代行,从第2行开始
				for (int i = 2; i < tb.numRows(); i++) {
					tr = tb.getRow(i);
					lineString = "";
					for (int j = 0; j < tr.numCells(); j++) {
						td = tr.getCell(j);// 取得单元格
						// 取得单元格的内容
						for (int k = 0; k < td.numParagraphs(); k++) {
							para = td.getParagraph(k);
							cellString = para.text();
							if (cellString != null
									&& cellString.compareTo("") != 0) {
								// 如果不trim,取出的内容后会有一个乱码字符
								cellString = cellString.trim() + "|";
							}
							lineString += cellString;
						}
					}
					// 去除字符串末尾的一个管道符
					if (lineString != null && lineString.compareTo("") != 0) {
						lineString = lineString.substring(0, lineString
								.length() - 1);
					}
					list.add(lineString);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	public static void main(String[] args) {
		List list = OperateWord.readWordCell("C:\\身份核查结果.doc");
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			String str = (String) iter.next();
			System.err.println(str);
		}
	}
}


 所需类库:

poi-3.8-beta5-20111217.jar
poi-scratchpad-3.8-beta5-20111217.jar

可以从http://poi.apache.org/下载。

单独测试没问题,但放到项目里,会报下面的错误,应该是由于类冲突引起的。

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.POIDocument.<init>(Lorg/apache/poi/poifs/filesystem/DirectoryNode;)V
 at org.apache.poi.hwpf.HWPFDocumentCore.<init>(HWPFDocumentCore.java:146)
 at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:218)
 at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:186)
 at cn.ccb.boup.util.poi.OperateWord.readWordCell(OperateWord.java:37)
 at cn.ccb.boup.util.poi.OperateWord.main(OperateWord.java:87)

解决办法是:打开项目的properties->java build path->order and export,发现poi的2个jar包放到了最后,把poi的包移到最上面。问题解决!

### 回答1: POI是Apache基金会的一个Java API,用于处理Microsoft Office格式的文件,包括Word文档中的表格数据。下面是使用POI提取Word文档表格数据的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; public class TableReader { public static void main(String[] args) throws IOException { File file = new File("example.docx"); FileInputStream fis = new FileInputStream(file); XWPFDocument document = new XWPFDocument(fis); for (XWPFTable table : document.getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { System.out.print(cell.getText() + "\t"); } System.out.println(); } } document.close(); fis.close(); } } ``` 在这个示例代码中,我们首先打开Word文档读取其中的表格数据。然后,我们使用POI的XWPFTable、XWPFTableRow和XWPFTableCell类来遍历表格中的每个单元格,并使用getText()方法提取单元格中的文本。最后,我们将提取的表格数据打印到控制台上。 请注意,此示例假定Word文档的文件格式为docx。如果你需要读取早期版本的Word文档,例如.doc格式的文件,你需要使用不同的POI类来处理不同的文件格式。 ### 回答2: POI是一个用于处理Microsoft Office格式文件(如Word、Excel和PowerPoint)的Java API。利用POI提取Word文档表格中的表格数据,可以按照以下步骤进行: 1.导入POI库:在Java项目中,首先需要导入POI的相关库文件,可以通过Maven或手动添加jar文件的方式引入POI库。 2.加载Word文档:使用POI的XWPFDocument类,加载需要处理的Word文档。 3.获取表格:通过XWPFDocument类的getTables()方法,可以获取Word文档中的所有表格对象,返回一个Table[]数组。 4.遍历表格:对于每个表格对象,可以使用foreach或for循环依次遍历。 5.获取行和列:对于每个表格对象,可以使用getTableRows()方法获取表格的所有行对象,返回一个List<XWPFTableRow>集合。对于每个行对象,可以使用getTableCells()方法获取表格的所有单元格对象,返回一个List<XWPFTableCell>集合。 6.提取数据:对于每个单元格对象,使用getText()方法可以获取单元格中的文本内容,将其保存到一个数据结构中,如Map、List或二维数组。 7.处理数据:根据需要进行数据的后续处理,如打印输出、存储到数据库或进行其他计算等。 8.关闭文档:处理完毕后,使用XWPFDocument对象的close()方法关闭文档。 使用POI提取Word文档表格中的表格数据,可以方便地进行数据解析和处理,适用于各种需要处理Word表格数据的应用场景。 ### 回答3: POI是一个Java库,可以用来操作Microsoft Office文件(如Word、Excel和PowerPoint)。在POI中,可以使用XWPFDocument类来读取和操作Word文档。 要提取Word文档中的表格数据,首先需要加载Word文档,并使用XWPFDocument类创建一个对象来表示该文档。然后,可以使用getTables()方法来获取文档中的所有表格。对于每个表格,可以使用getRows()方法获取表格中的所有行。对于每一行,再使用getTableCells()方法获取行中的所有单元格。 下面是一个简单的代码示例,演示如何使用POI提取Word文档表格中的表格数据: ``` import org.apache.poi.xwpf.usermodel.*; public class ExtractTableDataFromWord { public static void main(String[] args) { try { // 加载Word文档 XWPFDocument document = new XWPFDocument(new FileInputStream("example.docx")); // 获取文档中的所有表格 List<XWPFTable> tables = document.getTables(); // 遍历每个表格 for (XWPFTable table : tables) { // 获取表格中的所有行 List<XWPFTableRow> rows = table.getRows(); // 遍历每一行 for (XWPFTableRow row : rows) { // 获取行中的所有单元格 List<XWPFTableCell> cells = row.getTableCells(); // 遍历每个单元格,并打印单元格的值 for (XWPFTableCell cell : cells) { System.out.println(cell.getText()); } } } // 关闭文档 document.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码将读取名为"example.docx"的Word文档中的所有表格,并将每个单元格的值打印到控制台上。这个例子已经简化,如果需要更详细的操作,可以参考POI的官方文档或其他资源。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值