码迷,mamicode.com
首页 > 编程语言 > 详细

《《《java代码读取word文档》》》

时间:2020-04-15 21:39:56      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:tor   poi   ===   close   docx   com   test   pac   apache   

参考网址 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 }

 

《《《java代码读取word文档》》》

标签:tor   poi   ===   close   docx   com   test   pac   apache   

原文地址:https://www.cnblogs.com/lidar/p/12708176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!