关于这七种文档,我相信应该是最常用的文档了
在以下的介绍中会提到POI,现介绍下POI吧
poi处理WORD,EXCEL比较好:http://jakarta.apache.org/poi/
poi处理至少需要如下几个JAR包
PDFbox处理PDF比较好:http://pdfbox.apache.org/download.html
下面一一介绍了
第一和第二是只支持03版的word和excel文档
第一、首先来看WORD文档:
我这里用的是poi,相关jar包自己去下载,然后加到工程中(以下所要用的jar包也是,不再重复说)
- <span style="font-size: medium;"><span style="font-size: large;">public static String readWord(String path) throws Exception {
- String bodyText = null;
- try {
- FileInputStream is = new FileInputStream(path);
- bodyText = new WordExtractor(is).getText();
- } catch (Exception e) {
- System.out.println("=======");
- }
- return bodyText;
- }</span></span>
第二、Exel的文档
- <span style="font-size: medium;"><span style="font-size: large;">public static String ReadExcel(String path) throws IOException {
- InputStream inputStream = null;
- String content = null;
- try {
- inputStream = new FileInputStream(path);
- HSSFWorkbook wb = new HSSFWorkbook(inputStream);
- ExcelExtractor extractor = new ExcelExtractor(wb);
- extractor.setFormulasNotResults(true);
- extractor.setIncludeSheetNames(false);
- content = extractor.getText();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return content;
- }</span></span>
针对07版的word和excel的操作
package com.test;
第三、PowerPoint的文档
- <span style="font-size: medium;"><span style="font-size: large;">public static String readPowerPoint(String path) {
- StringBuffer content = new StringBuffer("");
- try {
- SlideShow ss = new SlideShow(new HSLFSlideShow(new FileInputStream(
- path)));
-
- Slide[] slides = ss.getSlides();
- for (int i = 0; i < slides.length; i++) {
- TextRun[] t = slides[i].getTextRuns();
- for (int j = 0; j < t.length; j++) {
- content.append(t[j].getText());
- }
- }
- } catch (Exception ex) {
- System.out.println(ex.toString());
- }
- return content.toString();
- }</span></span>
第四、PDF的文档
- <span style="font-size: medium;"><span style="font-size: large;">public static String readPdf(String path) throws Exception {
- StringBuffer content = new StringBuffer("");
- FileInputStream fis = new FileInputStream(path);
- PDFParser p = new PDFParser(fis);
- p.parse();
- PDFTextStripper ts = new PDFTextStripper();
- content.append(ts.getText(p.getPDDocument()));
- fis.close();
- return content.toString().trim();
- }</span></span>
第五、HTML的文档,要说明的是,HTML文档我们要获取其TITLE,BODY中的内容就要先获取源文件,然后再对源文件进行标签上的过滤,很麻烦
- <span style="font-size: large;">public static String readHtml(String urlString) {
- StringBuffer content = new StringBuffer("");
- File file = new File(urlString);
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- fis, "utf-8"));
- String line = null;
- while ((line = reader.readLine()) != null) {
- content.append(line + "\n");
- }
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- String contentcontentString = content.toString();
- String htmlStr = contentString; // 含html标签的字符串
- String textStr = "";
- java.util.regex.Pattern p_script;
- java.util.regex.Matcher m_script;
- java.util.regex.Pattern p_style;
- java.util.regex.Matcher m_style;
- java.util.regex.Pattern p_html;
- java.util.regex.Matcher m_html;
- try {
- String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\
- String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*
- String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
- p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
- m_script = p_script.matcher(htmlStr);
- htmlStr = m_script.replaceAll(""); // 过滤script标签
- p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
- m_style = p_style.matcher(htmlStr);
- htmlStr = m_style.replaceAll(""); // 过滤style标签
- p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
- m_html = p_html.matcher(htmlStr);
- htmlStr = m_html.replaceAll(""); // 过滤html标签
- textStr = htmlStr;
- } catch (Exception e) {
- System.err.println("Html2Text: " + e.getMessage());
- }
- return textStr;// 返回文本字符串
- }</span>
第六、TXT的文档,给TXT文本建立索引时要注意
本项目实现了组合查询的功能
//这一步如果不设置为GBK,TXT内容将全部乱码 BufferedReader reader=new BufferedReader(new InputStreamReader(is,"GBK")); 具体代码如下
- <span style="font-size: medium;"><span style="font-size: large;">public static String readTxt(String path) throws IOException {
- StringBuffer sb = new StringBuffer("");
- InputStream is = new FileInputStream(path);
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,
- "GBK"));
- try {
- String line = "";
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\r");
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return sb.toString().trim();
- }</span></span>
第七、RTF文档,rtf的转换则在javax中就有
- <span style="font-size: medium;"><span style="font-size: large;">public static String readRtf(String path) {
- String result = null;
- File file = new File(path);
- try {
- DefaultStyledDocument styledDoc = new DefaultStyledDocument();
- InputStream is = new FileInputStream(file);
- new RTFEditorKit().read(is, styledDoc, 0);
- result = new String(styledDoc.getText(0, styledDoc.getLength())
- .getBytes("iso8859-1"), "gbk");
-
- } catch (IOException e) {
- e.printStackTrace();
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
- return result;
- }</span></span>
分享到:
相关推荐
JAVA读取WORD_EXCEL_POWERPOINT_PDF文件的方法(poi)
JAVA读取PDF、WORD、EXCEL等文件的方法
该项目完美实现word、pdf、txt、excel的读取功能
最近在做一个简历管理工具时遇到了一些需求,在网上找了大量资源信息,发现Qt对pdf、word进行数据读取的信息少之又少,于是根据我自己的摸索,实现了这些需求功能。 功能主要实现了1、对pdf、word文件进行预览显示2...
c# 机器里无需安装word excel等office软件,就可以读取和生成word excel powerpoint等office文档,相当的强悍,生成excel时还可以生成图形。
支持excel2003,2007的解析读取,可指定sheet页读取,指定行开始读取,过滤空行,指定列读取,过滤空单元格,支持json文件的解析读取,支持word文件的解析读取,txt文件等等,需要API文档可私信本人
JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
包括中文字符处理,直接是个工程包,可以直接用, 主要用到的包 *需要引入的包(附件中都在) ... * itextpdf-5.4.2.jar * pdfbox-app-1.8.2.jar * antlr-2.7.6.jar * chardet-1.0.jar * cpdetector_1.0.8.jar
Java读取Word中的表格(Excel),并导出文件为Excel
1、源码环境为vs2005 2、word、excel文件生成html,使用office的owc11,需要添加com引用。...3、读取word、ppt、pdf的内容,生成txt,读取pdf需要添加引用PDFBox-0.7.3.dll(bin目录下有此文件)。
VC读取Word文档,VC读取Word文档很实用
支持在线或本地读取word、ppt、pdf、excel、doc等文件(亲测亲用,OOK哒)
整理了用java如何读取word文档,pdf文档的几种方法,含有程序
Java通过POI读取Excel遍历数据,根本word模板批量生成word文档,demo可运行,可根据需求修改
C#操作word文档 C#实现Word中表格信息读取
aspose.word 解析word读取及word转pdf、转html;附带文件;需要的下载
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
可以完整的读取word文件Excel文件PDF文件Txt文件,以文本的形式读出来,简单易懂
java版本,读取doc、rtf文档内容,解决中文乱码问题,log4j日志记录,使用swing进行rtf内容读取
本资源为一个无需office环境,使用开源库NPOI操作word、excel的样例程序,使用C#语言。内含NPOI库,可直接运行。在vs2019中测试通过。 程序实现的功能: 读取excel表格中的数据, 将数据转换成datatable, ...