使用openXML获取自定义属性和版本

时间:2017-05-30 09:52:43

标签: c# excel openxml openxml-sdk

我在具有一些自定义属性的特定版本中有一个OpenXML-Excel文档。当我查看该文件的xml结构时,这些属性存储在.\customXml\item2.xml

(部分)

<xsd:all>
<xsd:element ref="ns2:Status"/>
<xsd:element ref="ns2:Duedate" minOccurs="0"/>
<xsd:element ref="ns2:SpId" minOccurs="0"/>
<xsd:element ref="ns2:TemplateName" minOccurs="0"/>
<xsd:element ref="ns2:MailSent" minOccurs="0"/>
<xsd:element ref="ns2:Company" minOccurs="0"/>
<xsd:element ref="ns2:WeeklyReminderSent" minOccurs="0"/>
<xsd:element ref="ns2:DailyReminderSent" minOccurs="0"/>
</xsd:all>

此外,此文件还启用了修订。

现在我需要使用OpenXML(或ClosedXML)

读取这些值

我试图跟踪接收这些数据。 ClosedXML:

var workBook = new ClosedXML.Excel.XLWorkbook("myfile.xlsx");
var props = workBook.Properties;
var custProps = workBook.CustomProperties;

的OpenXML:

using (FileStream fs = new FileStream("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read))
{
  using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
  {
    var coreprops = doc.CoreFilePropertiesPart;
    var custprops = doc.CustomFilePropertiesPart;
    var extprops = doc.ExtendedFilePropertiesPart;
  }
}

两种方法都不包含任何对象(props,custprops,coreprops,extrprops)中存储的任何自定义属性

如何通过OpenXML-Way收集这些信息?

1 个答案:

答案 0 :(得分:-1)

使用OpenXML sdk 2.5

您获得了CustomPropertiesPart,但您需要从中获取属性:

CustomFilePropertiesPart customPropsPart = doc.CustomFilePropertiesPart;
DocumentFormat.OpenXml.CustomProperties.Properties props = customPropsPart.Properties;

foreach (DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty prop in props)
{
  //do what you want to do... in my case I have only one Text property...
  String propName = prop.Name;
  String value = prop.VTLPWSTR.InnerText;
}