参考网址 http://www.2cto.com/kf/201701/584974.html
1 package com.demo.test; 2 3 import org.apache.poi.hwpf.extractor.WordExtractor; 4 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 10 public class readWord { 11 12 public static void main(String[] args) { 13 ExportDocImpl test = new ExportDocImpl(); 14 //String filePath="E:\java导入word表格.doc"; 15 String filePath="E:\java导入word表格.docx"; 16 // test.testWord(filePath); 17 getTextFromWord(filePath); 18 } 19 /** 20 * 21 * @Title: getTextFromWord 22 * @Description: 读取word 23 * @param filePath 24 * 文件路径 25 * @return: String 读出的Word的内容 26 */ 27 public static String getTextFromWord(String filePath) { 28 String result = null; 29 // filePath="E:\java导入word表格.docx"; 30 File file = new File(filePath); 31 FileInputStream fis = null; 32 try { 33 fis = new FileInputStream(file); 34 @SuppressWarnings("resource") 35 WordExtractor wordExtractor = new WordExtractor(fis); 36 result = wordExtractor.getText(); 37 System.err.println("result"+ "========================= "+result); 38 } catch (FileNotFoundException e) { 39 e.printStackTrace(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } finally { 43 if (fis != null) { 44 try { 45 fis.close(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 } 49 } 50 } 51 return result; 52 } 53 }