您的位置:首页 > 运维架构

OpenXML SDK 读取WORD文档中的内容控件

2017-03-22 15:28 633 查看
OpenXML SDK 是不是已经开源了?原先实在微软的网站上下载最新版是2.5

BING查询他的网址https://github.com/OfficeDev/Open-Xml-Sdk

最新版是2.7.1,由于他采用了最新的.net core使用vs2015打不开,因此忽略他的最新版。

使用2.6版,在NuGet上发现有2.6.1版,但是在他的网站上没看到,只有2.7.1 2.7 2.6 2.5.1 2.5。

using System;

using System.Collections.Generic;

using System.Linq;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

using DocumentFormat.OpenXml.Office2010.Word;

namespace TestPaper

{

    sealed class DocxPaper : IPaper

    {

        public bool Checking(string sFileName)

        {

            string tempstr;

            using (WordprocessingDocument doc = WordprocessingDocument.Open(sFileName, false))

            {

                MainDocumentPart mpart = doc.MainDocumentPart;

                foreach(SdtContentCheckBox chk in mpart.Document.Descendants<SdtContentCheckBox>().ToList())

                {

                    var tagProperty = chk.Parent.Descendants<Tag>().FirstOrDefault();
//这是CheckBox内容控件的标记,VBA中的tag属性

                    if(tagProperty != null)

                    {

                        tempstr = tagProperty.Val;

                    }

                    tempstr = chk.Checked.Val;

                    if(chk.Checked.Val == OnOffValues.True)
//此时checkbox选中状态

                    {

                        tempstr = "1"; //这是测试代码无用,为了断点能调试

                    }

                }

            }

            return true;

        }

    }

}

忽略接口IPaper,这是项目中使用的。只定义了方法!

这应该是比较快的了,除非使用C++了。我以前的文章有C++访问xlsx!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: