以下是Java对几种文本文件内容读取代码。其中,OFFICE文档(WORD,EXCEL)使用了POI控件,PDF使用了PDFBOX控件。
点击这里 查看相关控件的下载地址和配置方法。
WORD
EXCEL
TXT
RTF
HTML
注意 :若使用WPS编辑相关文档,会有错误提示,应避免。
错误文本提示如下:
WORD
Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong!
EXCEL
java.lang.RuntimeException: Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord)
at org.apache.poi.hssf.model.LinkTable.readExtSheetRecord(LinkTable.java:187)
at org.apache.poi.hssf.model.LinkTable.<init>(LinkTable.java:163)
at org.apache.poi.hssf.model.Workbook.createWorkbook(Workbook.java:199)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:273)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:196)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:312)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:293)
at textReader.ExcelReader.getTextFromExcel(ExcelReader.java:23)
at DocumentInfo.getContent(DocumentInfo.java:86)
at MainFunction.main(MainFunction.java:19)
RTF
java.io.IOException: Too many close-groups in RTF text
at javax.swing.text.rtf.RTFParser.write(Unknown Source)
at javax.swing.text.rtf.RTFParser.writeSpecial(Unknown Source)
at javax.swing.text.rtf.AbstractFilter.write(Unknown Source)
at javax.swing.text.rtf.AbstractFilter.readFromStream(Unknown Source)
at javax.swing.text.rtf.RTFEditorKit.read(Unknown Source)
at textReader.RtfReader.getTextFromRtf(RtfReader.java:25)
at DocumentInfo.getContent(DocumentInfo.java:74)
at MainFunction.main(MainFunction.java:19)
BODY { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } P { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } DIV { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } TD { FONT-FAMILY:Tahoma; FONT-SIZE:10pt } 顺便说一下,这里为什么会在write出错呢?因为 level是根据{和}来进行自增和自减的,当括号不匹配的时候就会提示该错误。 wps编辑rtf文件在格式上出了问题,{和}不匹配。 但用word或写字板下新建文件,编辑后另存为rtf文件(wps下不支持),用记事本打开可以发现添加了很多格式说明,但是{和}是匹配的,这样才不会报错。下面是具体说明:
http://www.chinaitpower.com/source/jdk122/javax/swing/text/rtf/RTFParser.java.html