(3)POI读取word表格内容
package PoiTest;
import java.io.FileInputStream;
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;
public class ExportDocImpl {
public static void testWord() {
try {
FileInputStream in = new FileInputStream(“D:\\table.doc”);// 载⼊⽂档
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = Range();// 得到⽂档的读取范围
TableIterator it = new TableIterator(range);
// 迭代⽂档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
/
/ 迭代⾏,默认从0开始
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 迭代列,默认从0开始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);
// System.out.());
// 取得单元格的内容
for (int k = 0; k < td.numParagraphs(); k++) {
Paragraph para = td.getParagraph(k);
String s = ();
System.out.placeAll(“\r”, “”).replaceAll(” “,”")+”:”+s2.replaceAll(“\r”, “”).replaceAll(” “,”")); }
replaceall()}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
testWord();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论