zoukankan      html  css  js  c++  java
  • 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(); } }
  • 相关阅读:
    K8s 使用 nfs-client-provisioner
    MySQL IF CASE 例子
    Nginx 限速
    Python 元组操作
    Python if, while,for,continue,break,三目运算符
    Centos7 安装 pyenv
    MySQL 查看大事务
    Tomcat 修改日志路径及日志分割
    游戏攻略 美少女万华镜5
    自建远程桌面过程 vnc + frp
  • 原文地址:https://www.cnblogs.com/ssyh/p/10598740.html
Copyright © 2011-2022 走看看