完成[抽取七种文件格式内容]工具类

This commit is contained in:
2023-04-21 15:20:00 +08:00
parent 8fd3079588
commit b3b6a47aa3
2 changed files with 136 additions and 0 deletions
@@ -0,0 +1,16 @@
package com.adc.da.report.constant;
/**
* @author ThinkBook
*/
public class ReportConstants {
public static final String FILE_EXTENSIONS_DOC = "doc";
public static final String FILE_EXTENSIONS_DOCX = "docx";
public static final String FILE_EXTENSIONS_XLS = "xls";
public static final String FILE_EXTENSIONS_XLSX = "xlsx";
public static final String FILE_EXTENSIONS_PPT = "ppt";
public static final String FILE_EXTENSIONS_PPTX = "pptx";
public static final String FILE_EXTENSIONS_PDF = "pdf";
}
@@ -0,0 +1,120 @@
package com.adc.da.report.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.adc.da.report.constant.ReportConstants;
import org.apache.poi.hslf.usermodel.HSLFShape;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFTextShape;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
/**
* 提取文件中的内容
*
* @author caihaohan
*/
public class FileContentUtil {
public static String readFileContent(File file) throws IOException {
String fileName = file.getName();
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
StringBuilder content = new StringBuilder();
FileInputStream fis = new FileInputStream(file);
switch (fileExtension) {
case ReportConstants.FILE_EXTENSIONS_XLS:
HSSFWorkbook workbookXls = new HSSFWorkbook(fis);
content = new StringBuilder(readExcelContent(workbookXls));
break;
case ReportConstants.FILE_EXTENSIONS_XLSX:
XSSFWorkbook workbookXlsx = new XSSFWorkbook(fis);
content = new StringBuilder(readExcelContent(workbookXlsx));
break;
case ReportConstants.FILE_EXTENSIONS_DOC:
HWPFDocument documentDoc = new HWPFDocument(fis);
WordExtractor extractorDoc = new WordExtractor(documentDoc);
content = new StringBuilder(extractorDoc.getText());
break;
case ReportConstants.FILE_EXTENSIONS_DOCX:
XWPFDocument documentDocx = new XWPFDocument(fis);
XWPFWordExtractor extractorDocx = new XWPFWordExtractor(documentDocx);
content = new StringBuilder(extractorDocx.getText());
break;
case ReportConstants.FILE_EXTENSIONS_PPT:
HSLFSlideShow ppt = new HSLFSlideShow(fis);
for (HSLFSlide slide : ppt.getSlides()) {
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape textShape = (HSLFTextShape) shape;
content.append(textShape.getText()).append("\n");
}
}
}
break;
case ReportConstants.FILE_EXTENSIONS_PPTX:
XMLSlideShow pptx = new XMLSlideShow(fis);
for (XSLFSlide slide : pptx.getSlides()) {
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
content.append(textShape.getText()).append("\n");
}
}
}
break;
case ReportConstants.FILE_EXTENSIONS_PDF:
PDDocument document = PDDocument.load(fis);
PDFTextStripper stripper = new PDFTextStripper();
content = new StringBuilder(stripper.getText(document));
default:
}
fis.close();
return content.toString();
}
private static String readExcelContent(org.apache.poi.ss.usermodel.Workbook workbook) {
StringBuilder sb = new StringBuilder();
int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(i);
for (Row row : sheet) {
for (Cell cell : row) {
sb.append(cell.toString());
sb.append("\t");
}
sb.append("\n");
}
}
return sb.toString();
}
public static void main(String[] args) {
File file = new File("C:\\Users\\ThinkBook\\Desktop\\hzwl-projcets\\测试文件\\testppt.ppt");
try {
String content = FileContentUtil.readFileContent(file);
System.out.println("File content: " + content);
} catch (IOException e) {
e.printStackTrace();
}
}
}