java利用POI實現讀取Word並獲取指定樣式的文本


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.*;

public class WordToDB {

    public static void main(String[] args) throws Exception {
        String filePath = "***.doc";
        printWord(filePath);       
    }

    public static void printWord(String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        HWPFDocument doc = new HWPFDocument(is);
        Range r = doc.getRange();// 文檔范圍

        // System.out.println("段落數:"+r.numParagraphs());

        for (int i = 0; i < r.numParagraphs(); i++) {

            
            Paragraph p = r.getParagraph(i);// 獲取段落
        

int numStyles = doc.getStyleSheet().numStyles(); int styleIndex = p.getStyleIndex(); if (numStyles > styleIndex) { StyleSheet style_sheet = doc.getStyleSheet(); StyleDescription style = style_sheet.getStyleDescription(styleIndex); String styleName = style.getName();// 獲取每個段落樣式名稱 // 獲取自己理想樣式的段落文本信息 String styleLoving = "級別2:四號黑體 20磅 前18 后12 左對齊"; if (styleName != null && styleName.contains(styleLoving)) { String text = p.text();// 段落文本 System.out.println(text); } } } doc.close(); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM