发表新主题 回复该帖子
主题:[转]JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
唧唧
帖子档案  楼主 [转]JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例   Post by : 2009-10-16 15:34:18.0
  • 幼儿园
  • 幼儿园
  • UID:3
  • 主题:342
  • 帖子:781
  • 加为好友 加为好友    发送短信 发送短信

以下是Java对几种文本文件内容读取代码。其中,OFFICE文档(WORD,EXCEL)使用了POI控件,PDF使用了PDFBOX控件。

相关控件的下载地址和配置方法 见2F。

Word:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7.  
  8. import org.apache.poi.hwpf.extractor.WordExtractor;  
  9.  
  10. public class WordReader {  
  11.     public WordReader() {  
  12.     }  
  13.  
  14.     /**  
  15.      * @param filePath  
  16.      *            文件路径  
  17.      * @return 读出的Word的内容  
  18.      */ 
  19.     public String getTextFromWord(String filePath) {  
  20.         String result = null;  
  21.         File file = new File(filePath);  
  22.         try {  
  23.             FileInputStream fis = new FileInputStream(file);  
  24.             WordExtractor wordExtractor = new WordExtractor(fis);  
  25.             result = wordExtractor.getText();  
  26.         } catch (FileNotFoundException e) {  
  27.             e.printStackTrace();  
  28.         } catch (IOException e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         ;  
  32.         return result;  
  33.     }  

Excel:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6.  
  7. import org.apache.poi.hssf.usermodel.HSSFCell;  
  8. import org.apache.poi.hssf.usermodel.HSSFRow;  
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  11.  
  12. public class ExcelReader {  
  13.  
  14.     /*  
  15.      * @param filePath 文件路径  
  16.      *   
  17.      * @return 读出的Excel的内容  
  18.      */ 
  19.     public String getTextFromExcel(String filePath) {  
  20.         StringBuffer buff = new StringBuffer();  
  21.         try {  
  22.             // 创建对Excel工作簿文件的引用  
  23.             HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));  
  24.             // 创建对工作表的引用。  
  25.             for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {  
  26.                 if (null != wb.getSheetAt(numSheets)) {  
  27.                     HSSFSheet aSheet = wb.getSheetAt(numSheets);// 获得一个sheet  
  28.                     for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {  
  29.                         if (null != aSheet.getRow(rowNumOfSheet)) {  
  30.                             HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行  
  31.                             for (int cellNumOfRow = 0; cellNumOfRow <= aRow  
  32.                                     .getLastCellNum(); cellNumOfRow++) {  
  33.                                 if (null != aRow.getCell(cellNumOfRow)) {  
  34.                                     HSSFCell aCell = aRow.getCell(cellNumOfRow);// 获得列值  
  35.                                     switch (aCell.getCellType()) {  
  36.                                     case HSSFCell.CELL_TYPE_FORMULA:  
  37.                                         break;  
  38.                                     case HSSFCell.CELL_TYPE_NUMERIC:  
  39.                                         buff.append(aCell.getNumericCellValue()).append('\t');  
  40.                                         break;  
  41.                                     case HSSFCell.CELL_TYPE_STRING:  
  42.                                         buff.append(aCell.getStringCellValue()).append('\t');  
  43.                                         break;  
  44.                                     }  
  45.                                 }  
  46.                             }  
  47.                             buff.append('\n');  
  48.                         }  
  49.                     }  
  50.                 }  
  51.             }  
  52.         } catch (FileNotFoundException e) {  
  53.             e.printStackTrace();  
  54.         } catch (IOException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.         return buff.toString();  
  58.     }  

Pdf:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6.  
  7. import org.pdfbox.pdfparser.PDFParser;  
  8. import org.pdfbox.pdmodel.PDDocument;  
  9. import org.pdfbox.util.PDFTextStripper;  
  10.  
  11. public class PdfReader {  
  12.     public PdfReader() {  
  13.     }  
  14.  
  15.     /**  
  16.      * @param filePath  
  17.      *            文件路径  
  18.      * @return 读出的pdf的内容  
  19.      */ 
  20.     public String getTextFromPdf(String filePath) {  
  21.         String result = null;  
  22.         FileInputStream is = null;  
  23.         PDDocument document = null;  
  24.         try {  
  25.             is = new FileInputStream(filePath);  
  26.             PDFParser parser = new PDFParser(is);  
  27.             parser.parse();  
  28.             document = parser.getPDDocument();  
  29.             PDFTextStripper stripper = new PDFTextStripper();  
  30.             result = stripper.getText(document);  
  31.         } catch (FileNotFoundException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         } finally {  
  36.             if (is != null) {  
  37.                 try {  
  38.                     is.close();  
  39.                 } catch (IOException e) {  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }  
  43.             if (document != null) {  
  44.                 try {  
  45.                     document.close();  
  46.                 } catch (IOException e) {  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         }  
  51.         return result;  
  52.     }  
  53.  

Txt:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5.  
  6. public class TxtReader {  
  7.     public TxtReader() {  
  8.     }  
  9.  
  10.     /**  
  11.      * @param filePath  
  12.      *            文件路径  
  13.      * @return 读出的txt的内容  
  14.      */ 
  15.     public String getTextFromTxt(String filePath) throws Exception {  
  16.  
  17.         FileReader fr = new FileReader(filePath);  
  18.         BufferedReader br = new BufferedReader(fr);  
  19.         StringBuffer buff = new StringBuffer();  
  20.         String temp = null;  
  21.         while ((temp = br.readLine()) != null) {  
  22.             buff.append(temp + "\r\n");  
  23.         }  
  24.         br.close();  
  25.         return buff.toString();  
  26.     }  

Rtf:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.  
  8. import javax.swing.text.BadLocationException;  
  9. import javax.swing.text.DefaultStyledDocument;  
  10. import javax.swing.text.rtf.RTFEditorKit;  
  11.  
  12. public class RtfReader {  
  13.     public RtfReader() {  
  14.     }  
  15.  
  16.     /**  
  17.      * @param filePath  
  18.      *            文件路径  
  19.      * @return 读出的rtf的内容  
  20.      */ 
  21.     public String getTextFromRtf(String filePath) {  
  22.         String result = null;  
  23.         File file = new File(filePath);  
  24.         try {  
  25.             DefaultStyledDocument styledDoc = new DefaultStyledDocument();  
  26.             InputStream is = new FileInputStream(file);  
  27.             new RTFEditorKit().read(is, styledDoc, 0);  
  28.             result = new String(styledDoc.getText(0, styledDoc.getLength())  
  29.                     .getBytes("ISO8859_1"));  
  30.             // 提取文本,读取中文需要使用ISO8859_1编码,否则会出现乱码  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         } catch (BadLocationException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return result;  
  37.     }  
  38.  

Html:

   java代码
  1. package textReader;  
  2.  
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8.  
  9. public class HtmlReader {  
  10.     public HtmlReader() {  
  11.     }  
  12.  
  13.     /**  
  14.      * @param filePath  
  15.      *            文件路径  
  16.      * @return 获得html的全部内容  
  17.      */ 
  18.     public String readHtml(String filePath) {  
  19.         BufferedReader br = null;  
  20.         StringBuffer sb = new StringBuffer();  
  21.         try {  
  22.             br = new BufferedReader(new InputStreamReader(new FileInputStream(  
  23.                     filePath), "GB2312"));  
  24.             String temp = null;  
  25.             while ((temp = br.readLine()) != null) {  
  26.                 sb.append(temp);  
  27.             }  
  28.         } catch (FileNotFoundException e) {  
  29.             e.printStackTrace();  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         return sb.toString();  
  34.     }  
  35.  
  36.     /**  
  37.      * @param filePath  
  38.      *            文件路径  
  39.      * @return 获得的html文本内容  
  40.      */ 
  41.     public String getTextFromHtml(String filePath) {  
  42.         // 得到body标签中的内容  
  43.         String str = readHtml(filePath);  
  44.         StringBuffer buff = new StringBuffer();  
  45.         int maxindex = str.length() - 1;  
  46.         int begin = 0;  
  47.         int end;  
  48.         // 截取>和<之间的内容  
  49.         while ((begin = str.indexOf('>', begin)) < maxindex) {  
  50.             end = str.indexOf('<', begin);  
  51.             if (end - begin > 1) {  
  52.                 buff.append(str.substring(++begin, end));  
  53.             }  
  54.             begin = end + 1;  
  55.         }  
  56.         ;  
  57.         return buff.toString();  
  58.     }  
  59.  

 

签名
 ★★★★★★★★
 纵里寻她千百度,蓦然回首,那人却在,灯火阑珊处!
 MyBlog :http://blog.javawind.net
返回页面顶部  

唧唧
2F POI,PDFBOX,JDOM的下载地址和在Eclipse中的配置方法   Post by : 2009-10-16 15:40:06.0
  • 幼儿园
  • 幼儿园
  • UID:3
  • 主题:342
  • 帖子:781
  • 加为好友 加为好友    发送短信 发送短信

一、POI
POI是Apache的Jakata项目,POI 代表 Poor Obfuscation Implementation,即不良模糊化实现。POI 的目标就是提供一组 Java API 来使得基于 Microsoft OLE 2 Compound Document 格式的 Microsoft Office 文件易于操作。

下载地址 :http://apache.etoak.com/poi/release/bin/

相关配置 :
(1) 把下载的 poi-bin-3.5-FINAL-20090928.tar.gz 解压。
(2) 将以下四个jar包拷入项目的lib文件夹下(如还未建立lib目录,则先创建一个, 不一定要是这个位置,只要classpath能找到jar就可以)。
poi-3.5-FINAL-20090928.jar
poi-contrib-3.5-FINAL-20090928.jar
poi-ooxml-3.5-FINAL-20090928.jar
poi-scratchpad-3.5-FINAL-20090928.jar
(3) 在工程上单击右键,在弹出的快捷菜单中选择“Build Path->Config Build Path->Add Jars”命令进行添加。
(4)在Order and Export标签页勾选库文件。

二、PDFBOX
PDFBOX是一个为开发人员读取和创建PDF文档而准备的纯Java类库。

下载地址:http://sourceforge.net/projects/pdfbox/

相关配置:
(1)把下载的 PDFBox-0.7.3.zip 解压
(2)进入external目录,这里包括了PDFBox所有用到的外部包。复制以下Jar包到工程lib目录下
bcmail-jdk14-132.jar
bcprov-jdk14-132.jar
checkstyle-all-4.2.jar
FontBox-0.1.0-dev.jar
lucene-core-2.0.0.jar
(3)然后再从PDFBox的lib目录下,复制PDFBox-0.7.3.jar到工程的lib目录下
(4)在工程上单击右键,在弹出的快捷菜单中选择“Build Path->Config Build Path->A5dd Jars”命令,把工程lib目录下面的包都加入工程的Build Path。
( 5 )在Order and Export标签页勾选库文件。

 三、JDOM
JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析、生成、序列化以及多种操作。
下载地址 : http://www.jdom.org/downloads/index.html 
相关配置 :
(1)把下载的jdom-1.1.1.tar.gz 解压
(2) 进入build目录 ,把jdom.jar 拷入项目的lib文件夹下
(3) 在工程上单击右键,在弹出的快捷菜单中选择“Build Path->Config Build Path->Add Jars”命令进行添加。
(4)在Order and Export标签页勾选库文件。

签名
 ★★★★★★★★
 纵里寻她千百度,蓦然回首,那人却在,灯火阑珊处!
 MyBlog :http://blog.javawind.net
返回页面顶部  


CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 26 ms,0 (Queries)  Gzip enabled

WAP - 清除Cookies - 联系我们 - JavaWind.Net Studio - Archiver - TOP Valid XHTML 1.0 Transitional Valid CSS! 粤ICP备07511478号