Worksheet.CustomProperites OpenXML

我需要阅读Worksheet.CustomProperies 。 有什么方法可以阅读这个属性?

我也尝试使用工作簿和工作表获取XmlDocument

XmlDocument xlDoc = ws.WorksheetXml; 

给我:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <dimension ref="A3:K24" /> <sheetViews> <sheetView tabSelected="1" workbookViewId="0"><selection activeCell="H14" sqref="H14" /></sheetView> </sheetViews> <sheetFormatPr defaultRowHeight="15" /> <cols></cols><sheetData /> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3" /> <pageSetup orientation="portrait" horizontalDpi="4294967293" verticalDpi="4294967293" r:id="rId2" /> **<customProperties><customPr name="My_CustomProperty" r:id="rId3" /></customProperties>** </worksheet> 

我可以看到一个CustomProperty,但无法看到CustomProperty的值。 当我去CustomProperty bin文件(zip的xlsx和提取内容),价值在那里。

我已经在这里上传了这个文件

我不熟悉这些自定义属性,但这里是使用最新版本的EPPlus从示例文档中提取customProperty1.bin文件内容的一种方法:

 using (ExcelPackage p = new ExcelPackage(new FileInfo(@"C:\Users_Template_12_22_Template.xlsx"))) { var parts = p.Package.GetParts(); foreach (var part in parts) { if (part.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.customProperty") { using (var stream = part.GetStream()) { byte[] data = new byte[stream.Length]; stream.Read(data, 0, (int)stream.Length); stream.Close(); string customPropertyInfo = System.Text.Encoding.Unicode.GetString(data); } } } } 

如果您知道customProperty1.bin文件的名称/位置,则可以使用GetPart()而不是GetParts()来访问它:

 var u = new Uri("/xl/customProperty1.bin", UriKind.Relative); var part = p.Package.GetPart(u); 

请注意,您需要添加对WindowsBase.dll的引用(在“添加引用”中的“.NET”选项卡下)以使用与打包相关的方法。