You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Apache POI提取.doc/.docx文件的作者与创建日期元数据?

嗨,我来帮你搞定这个问题!其实不用提取全部元数据,Apache POI针对.doc和.docx分别有对应的API可以直接获取你要的作者和创建日期,我给你分两种情况整理了代码示例:

处理 .docx 文件(XWPF)

对于docx格式,我们可以通过XWPFDocument的核心属性对象直接定位到目标字段:

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.officeDocument.x2006.coreProperties.CoreProperties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class DocxMetadataExtractor {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("你的文件路径.docx");
             XWPFDocument doc = new XWPFDocument(fis)) {
            
            CoreProperties coreProps = doc.getProperties().getCoreProperties();
            
            // 直接获取作者
            String author = coreProps.getCreator();
            // 直接获取创建日期
            Date createDate = coreProps.getCreated();
            
            System.out.println("文档作者: " + author);
            System.out.println("创建日期: " + createDate);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

核心就是通过coreProps.getCreator()和coreProps.getCreated()精准提取你需要的两项元数据,完全不用管其他无关字段。

处理 .doc 文件(HWPF)

针对旧版的doc格式,需要用到HWPFDocument和SummaryInformation来获取信息:

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.SummaryInformation;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class DocMetadataExtractor {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("你的文件路径.doc");
             HWPFDocument doc = new HWPFDocument(fis)) {
            
            SummaryInformation summaryInfo = doc.getSummaryInformation();
            
            // 直接获取作者
            String author = summaryInfo.getAuthor();
            // 直接获取创建日期
            Date createDate = summaryInfo.getCreateDateTime();
            
            System.out.println("文档作者: " + author);
            System.out.println("创建日期: " + createDate);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这里通过summaryInfo.getAuthor()和summaryInfo.getCreateDateTime()就能快速拿到目标数据,是不是很省心?

另外提醒下,要确保项目里引入了对应的Apache POI依赖:docx需要poi-ooxml,doc需要poi-scratchpad,不然会出现类找不到的问题哦。

内容的提问来源于stack exchange,提问作者SaraH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:07:25