Java使用POI读取word中表格,及内容

今天领导给个需求,让做个文档,读取文档内容打印,直接上代码!

-----------------------------------------------------------------------------------------------------------分割线--------------------------------------------------------

package com.tristone.pmc.util;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
* @author: dw
* @time: 2022/3/22 13:22
*/

public class ExportDocImpl {

public static void main(String[] args){
ExportDocImpl ExportDocImpl = new ExportDocImpl();
ExportDocImpl.testWord();
}

public void testWord(){
try {
// 获取Word文件
InputStream is = new FileInputStream(new File("D:\\test.doc"));
// 读写word文档类
WordExtractor ex = new WordExtractor(is);
// 打印文件中文体
String str1 = ex.getText();
// 拿到数组
String[] str2 = ex.getParagraphText();
// 用下标拿到指定位置数据
System.out.println(str2[2].trim());

// 进行自定义分割
str1 = str1.replaceAll(":", "");
String text2003 = str1;
System.out.println(text2003);

} catch (Exception e) {
e.printStackTrace();
}
}
}

-----------------------------------------------------------------------------------------------------------分割线--------------------------------------------------------

如有帮助到请点个赞,谢谢 
posted @ 2022-03-22 13:46  d-w  阅读(1190)  评论(0编辑  收藏  举报