• 读取微软三剑客的内容创建信息


    在微软办公套件生成的文件里面(xlsx,docx,pptx文件),存放了文档内容的创建和修改信息(不同于文件创建和修改信息),包括内容创建者、创建时间、最后一次修改的时间和修改人。这些信息可以用文件管理器,右键-详细信息页面能查看到:

    那么,我们怎么把这些信息读出来呢?

    原理:

    2007版以上的三剑客文件,实际上是压缩后的XML文件,这些信息保存在docProps/core.xml中。

    该文件的内容大致如下:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties " 。。。>
        <dc:creator>ZJ1301</dc:creator>
        <cp:lastModifiedBy>ZJ1301-苏光杰</cp:lastModifiedBy>
        <dcterms:created xsi:type="dcterms:W3CDTF">2015-03-19T06:25:56Z</dcterms:created>
        <dcterms:modified xsi:type="dcterms:W3CDTF">2018-09-14T09:41:18Z</dcterms:modified>
    </cp:coreProperties>

    利用这一原理,我们将三剑客文件解压后,从docProps/core.xml中读取出来

    以下是根据这一原理编写的函数

    /// <summary>
            /// 读取文档的内容创建信息
            /// 支持microsoft2007版以上三剑客 xlsx, docx,pptx
            /// </summary>
            /// <param name="docxFile">microsoft2007版以上三剑客 xlsx, docx,pptx</param>
            /// <returns></returns>
            private CreateInfo GetCreateInfo(string docxFile)
            {
                
                CreateInfo info = new CreateInfo();
                using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(docxFile))
                {
                    zip.ExtractSelectedEntries("docProps/core.xml", Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
                    string file = Path.Combine(Environment.CurrentDirectory, "docProps", "core.xml");
                    if (File.Exists(file))
                    {
                        XElement root = XElement.Load(file);
                        foreach (var e in root.Elements())
                        {
                            if (e.Name.LocalName == "creator") info.Creator = e.Value;
                            else if (e.Name.LocalName == "lastModifiedBy") info.LastModifiedBy = e.Value;
                            else if (e.Name.LocalName == "created") info.Created = DateTime.Parse(e.Value);
                            else if (e.Name.LocalName == "modified") info.Modified = DateTime.Parse(e.Value);
                        }
                        Directory.Delete(Path.GetDirectoryName( file), true);
                    }
                    return info;
                }
            }
    
            private class CreateInfo
            {
                public string Creator { get; set; }
                public string LastModifiedBy { get; set; }
                public DateTime Created { get; set; }
                public DateTime Modified { get; set; }
            }

    代码中用到了DotNetZip, 需要引用NuGet寻找导入。

  • 相关阅读:
    HTML<lable for="">标签的for属性。
    Microsoft_Office_Word_遇到问题需要关闭。我们对此引起的不便表示抱歉,问题解决方案
    AnyChart的资料,以后看
    JQquery 鼠标悬浮提示
    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
    JQuery UI selectable
    SqlServer 动态添加服务器
    基于CyberGarage库的dlna开发(android)
    自定义实现圆形播放进度条(android,飞一般的感觉)
    Lance老师UI系列教程第一课>QQ设置界面的实现(android)
  • 原文地址:https://www.cnblogs.com/ccjungle/p/11076151.html
Copyright © 2020-2023  润新知