JAVA提取Word,Excel,PPT,PDF,TXT等文档文字内容

首先引入Maven库

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>2.0.4</version>
		</dependency>

public class ParseText {

	// 判断文档类型,调用不同的解析方法
	public static String parse(byte[] buffer, String suffix) {
		String text = "";
		switch (suffix) {
		case "doc":
			text = getTextFromWord(buffer);
			break;
		case "docx":
			text = getTextFromWord2007(buffer);
			break;
		case "xls":
			text = getTextFromExcel(buffer);
			break;
		case "xlsx":
			text = getTextFromExcel2007(buffer);
			break;
		case "ppt":
			text = getTextFromPPT(buffer);
			break;
		case "pptx":
			text = getTextFromPPT2007(buffer);
			break;
		case "pdf":
			text = getTextFormPDF(buffer);
			break;
		case "txt":
			text = getTextFormTxt(buffer);
			break;
		default:
			System.out.println("不支持解析的文档类型");
		}

		return text.replaceAll("\\s*", "");
	}

	// 读取Word97-2003的全部内容 doc
	private static String getTextFromWord(byte[] file) {
		String text = "";
		InputStream fis = null;
		WordExtractor ex = null;
		try {
			// word 2003: 图片不会被读取
			fis = new ByteArrayInputStream(file);
			ex = new WordExtractor(fis);
			text = ex.getText();
			ex.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

	// 读取Word2007+的全部内容 docx
	private static String getTextFromWord2007(byte[] file) {
		String text = "";
		InputStream fis = null;
		XWPFDocument doc = null;
		XWPFWordExtractor workbook = null;
		try {
			fis = new ByteArrayInputStream(file);
			doc = new XWPFDocument(fis);
			workbook = new XWPFWordExtractor(doc);
			text = workbook.getText();
			workbook.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

	// 读取Excel97-2003的全部内容 xls
	private static String getTextFromExcel(byte[] file) {
		InputStream is = null;
		HSSFWorkbook wb = null;
		String text = "";
		try {
			is = new ByteArrayInputStream(file);
			wb = new HSSFWorkbook(new POIFSFileSystem(is));
			ExcelExtractor extractor = new ExcelExtractor(wb);
			extractor.setFormulasNotResults(false);
			extractor.setIncludeSheetNames(false);
			text = extractor.getText();
			extractor.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	// 读取Excel2007+的全部内容 xlsx
	private static String getTextFromExcel2007(byte[] file) {
		InputStream is = null;
		XSSFWorkbook workBook = null;
		String text = "";
		try {
			is = new ByteArrayInputStream(file);
			workBook = new XSSFWorkbook(is);
			XSSFExcelExtractor extractor = new XSSFExcelExtractor(workBook);
			extractor.setIncludeSheetNames(false);
			text = extractor.getText();
			extractor.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	// 读取Powerpoint97-2003的全部内容 ppt
	private static String getTextFromPPT(byte[] file) {
		String text = "";
		InputStream fis = null;
		PowerPointExtractor ex = null;
		try {
			// word 2003: 图片不会被读取
			fis = new ByteArrayInputStream(file);
			ex = new PowerPointExtractor(fis);
			text = ex.getText();
			ex.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return text;
	}

	// 抽取幻灯片2007+全部内容 pptx
	private static String getTextFromPPT2007(byte[] file) {
		InputStream is = null;
		XMLSlideShow slide = null;
		String text = "";
		try {
			is = new ByteArrayInputStream(file);
			slide = new XMLSlideShow(is);
			XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(slide);
			text = extractor.getText();
			extractor.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	// 读取pdf文件全部内容 pdf
	private static String getTextFormPDF(byte[] file) {
		String text = "";
		PDDocument pdfdoc = null;
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(file);
			pdfdoc = PDDocument.load(is);
			PDFTextStripper stripper = new PDFTextStripper();
			text = stripper.getText(pdfdoc);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pdfdoc != null) {
					pdfdoc.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return text;
	}

	// 读取txt文件全部内容 txt
	private static String getTextFormTxt(byte[] file) {
		String text = "";
		try {
			String encoding = get_charset(file);
			text = new String(file, encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return text;
	}

	// 获得txt文件编码方式
	private static String get_charset(byte[] file) throws IOException {
		String charset = "GBK";
		byte[] first3Bytes = new byte[3];
		InputStream bis = null;
		try {
			boolean checked = false;
			bis = new ByteArrayInputStream(file);
			bis.mark(0);
			int read = bis.read(first3Bytes, 0, 3);
			if (read == -1)
				return charset;
			if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
				charset = "UTF-16LE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
				charset = "UTF-16BE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				charset = "UTF-8";
				checked = true;
			}
			bis.reset();
			if (!checked) {
				while ((read = bis.read()) != -1) {
					if (read >= 0xF0)
						break;
					if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
						break;
					if (0xC0 <= read && read <= 0xDF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
							// (0x80 - 0xBF),也可能在GB编码内
							continue;
						else
							break;
					} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							read = bis.read();
							if (0x80 <= read && read <= 0xBF) {
								charset = "UTF-8";
								break;
							} else
								break;
						} else
							break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				bis.close();
			}
		}
		return charset;
	}
}





	// 读取pdf文件
	private static String getTextFormPDF(byte[] file) {
		String text = "";
		PDDocument pdfdoc = null;
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(file);
			pdfdoc = PDDocument.load(is);
			PDFTextStripper stripper = new PDFTextStripper();
			text = stripper.getText(pdfdoc);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pdfdoc != null) {
					pdfdoc.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return text;
	}

	// 读取txt文件
	private static String getTextFormTxt(byte[] file) {
		String text = "";
		try {
			String encoding = get_charset(file);
			text = new String(file, encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return text;
	}
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个Java获取各种文档页数的工具类,使用了Apache POI和Apache PDFBox库: ``` import java.io.File; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.pdfbox.pdmodel.PDDocument; public class DocumentPageCount { public static int getExcelPageCount(String filePath) { try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); Workbook workbook = null; if (filePath.endsWith(".xls")) { workbook = new HSSFWorkbook(fis); } else if (filePath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(fis); } fis.close(); return workbook.getNumberOfSheets(); } catch (Exception e) { e.printStackTrace(); return 0; } } public static int getPptPageCount(String filePath) { try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); SlideShow slideshow = null; if (filePath.endsWith(".ppt")) { slideshow = new HSLFSlideShow(fis); } else if (filePath.endsWith(".pptx")) { slideshow = new XMLSlideShow(fis); } fis.close(); return slideshow.getSlides().size(); } catch (Exception e) { e.printStackTrace(); return 0; } } public static int getWordPageCount(String filePath) { try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); HWPFDocument doc = null; Range range = null; XWPFDocument docx = null; if (filePath.endsWith(".doc")) { doc = new HWPFDocument(fis); range = doc.getRange(); } else if (filePath.endsWith(".docx")) { docx = new XWPFDocument(fis); range = docx.getDocument().getBody().getDocumentContent().getEndOfContent().getRange(); } fis.close(); return range.numParagraphs(); } catch (Exception e) { e.printStackTrace(); return 0; } } public static int getPdfPageCount(String filePath) { try { File file = new File(filePath); PDDocument document = PDDocument.load(file); int pageCount = document.getNumberOfPages(); document.close(); return pageCount; } catch (Exception e) { e.printStackTrace(); return 0; } } } ``` 这个工具类包含了四个方法,分别用于获取ExcelPPTWordPDF文档的页数。您可以在自己的项目中使用这些方法来获取文档的页数。例如,要获取一个Excel文件的页数,可以调用`DocumentPageCount.getExcelPageCount("path/to/excel/file.xls")`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值