From b3b6a47aa3f8ce33fcb60fe49ca1493ca188c801 Mon Sep 17 00:00:00 2001 From: Mzaxd Date: Fri, 21 Apr 2023 15:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90[=E6=8A=BD=E5=8F=96=E4=B8=83?= =?UTF-8?q?=E7=A7=8D=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F=E5=86=85=E5=AE=B9?= =?UTF-8?q?]=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/report/constant/ReportConstants.java | 16 +++ .../adc/da/report/util/FileContentUtil.java | 120 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 adc-da-report/src/main/java/com/adc/da/report/constant/ReportConstants.java create mode 100644 adc-da-report/src/main/java/com/adc/da/report/util/FileContentUtil.java diff --git a/adc-da-report/src/main/java/com/adc/da/report/constant/ReportConstants.java b/adc-da-report/src/main/java/com/adc/da/report/constant/ReportConstants.java new file mode 100644 index 0000000..d1a2e3b --- /dev/null +++ b/adc-da-report/src/main/java/com/adc/da/report/constant/ReportConstants.java @@ -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"; + +} diff --git a/adc-da-report/src/main/java/com/adc/da/report/util/FileContentUtil.java b/adc-da-report/src/main/java/com/adc/da/report/util/FileContentUtil.java new file mode 100644 index 0000000..1ba8074 --- /dev/null +++ b/adc-da-report/src/main/java/com/adc/da/report/util/FileContentUtil.java @@ -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(); + } + } + +}