Java Word解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35001776/article/details/83057979

maven 依赖:
        <!-- poi -->
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.0</version>
        </dependency>
        
demo:
        try (InputStream is = new InputStream(new File(""))) {
            if (DOC.equalsIgnoreCase(fileType)) {
                HWPFDocument document = new HWPFDocument(is);
                StringBuilder text = document.getText();
                return text.toString();
            } else if (DOCX.equalsIgnoreCase(fileType)) {
                XWPFDocument document = new XWPFDocument(is);
                XWPFWordExtractor in = new XWPFWordExtractor(document);
                String text = in.getText();
                return text;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

猜你喜欢

转载自blog.csdn.net/qq_35001776/article/details/83057979