🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1.pom ~~~ <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.1.0</version> </dependency> ~~~ 2. 读取两种文件 ~~~ if (file.getName().endsWith("doc") || file.getName().endsWith("docx")) { String[] paragraphText = new String[0]; if (file.getName().endsWith("doc")) { is = new FileInputStream(file); WordExtractor ex = new WordExtractor(is); paragraphText = ex.getParagraphText(); } if (file.getName().endsWith("docx")) { OPCPackage opcPackage = POIXMLDocument.openPackage(file.getPath()); System.out.println(file.getPath()); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); String[] split = extractor.getText().split("\n"); // List<String[]> collect = Arrays.asList(split).stream().map(v -> v.split("\t")).collect(Collectors.toList()); List<String> collect = Arrays.asList(split).stream().flatMap(v -> Arrays.stream(v.split("\t"))).collect(Collectors.toList()); paragraphText = collect.toArray(new String[0]); extractor.close(); } ~~~