-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathExcelSlicerXmlSource.cs
More file actions
159 lines (151 loc) · 6.35 KB
/
ExcelSlicerXmlSource.cs
File metadata and controls
159 lines (151 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using OfficeOpenXml.Packaging;
using OfficeOpenXml.Utils.FileUtils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace OfficeOpenXml
{
internal class ExcelSlicerXmlSources : XmlHelper
{
const string _tableUId = "{3A4CF648-6AED-40f4-86FF-DC5316D8AED3}";
const string _pivotTableUId = "{A8765BA9-456A-4dab-B4F3-ACF838C121DE}";
internal List<ExcelSlicerXmlSource> _list = new List<ExcelSlicerXmlSource>();
internal ZipPackagePart _part;
internal ExcelSlicerXmlSources(XmlNamespaceManager nsm, XmlNode topNode, ZipPackagePart part) : base(nsm, topNode)
{
_part = part;
foreach (XmlNode node in GetNodes("d:extLst/d:ext"))
{
switch (node.Attributes["uri"].Value)
{
case _tableUId: //Table slicer
foreach (XmlNode slicerNode in node.SelectNodes("x14:slicerList/x14:slicer", NameSpaceManager))
{
_list.Add(new ExcelSlicerXmlSource(eSlicerSourceType.Table, part, slicerNode.Attributes["r:id"].Value));
}
break;
case _pivotTableUId: //Pivot table slicer
foreach (XmlNode slicerNode in node.SelectNodes("x14:slicerList/x14:slicer", NameSpaceManager))
{
_list.Add(new ExcelSlicerXmlSource(eSlicerSourceType.PivotTable, part, slicerNode.Attributes["r:id"].Value));
}
break;
default:
break;
}
}
}
internal ExcelSlicerXmlSource GetOrCreateSource(eSlicerSourceType sourceType)
{
var src = GetSources(sourceType).FirstOrDefault();
if(src==null)
{
switch(sourceType)
{
case eSlicerSourceType.Table:
src=new ExcelSlicerXmlSource(eSlicerSourceType.Table, _part, null);
_list.Add(src);
break;
case eSlicerSourceType.PivotTable:
src = new ExcelSlicerXmlSource(eSlicerSourceType.PivotTable, _part, null);
_list.Add(src);
break;
}
}
return src;
}
internal XmlNode GetSource(string name, eSlicerSourceType sourceType, out ExcelSlicerXmlSource source)
{
foreach (var s in GetSources(sourceType))
{
var n = s.XmlDocument.DocumentElement.SelectSingleNode($"x14:slicer[@name=\"{name}\"]", NameSpaceManager);
if (n != null)
{
source = s;
return n;
}
}
source = null;
return null;
}
private IEnumerable<ExcelSlicerXmlSource> GetSources(eSlicerSourceType sourceType)
{
return _list.Where(x => x.Type == sourceType);
}
internal void Save()
{
foreach(var xs in _list)
{
var stream = new StreamWriter(xs.Part.GetStream(FileMode.Create, FileAccess.Write));
xs.XmlDocument.Save(stream);
}
}
internal void Remove(ExcelSlicerXmlSource source)
{
_list.Remove(source);
_part.Package.DeletePart(source.Uri);
}
}
internal class ExcelSlicerXmlSource : ExcelXmlSource
{
internal ExcelSlicerXmlSource(eSlicerSourceType type, ZipPackagePart relPart, string relId) : base(relPart, relId)
{
Type = type;
}
public eSlicerSourceType Type { get; }
}
internal class ExcelXmlSource
{
internal ExcelXmlSource(ZipPackagePart relPart, string relId)
{
if (string.IsNullOrEmpty(relId))
{
Uri = XmlHelper.GetNewUri(relPart.Package, "/xl/slicers/slicer{0}.xml");
Part = relPart.Package.CreatePart(Uri, "application/vnd.ms-excel.slicer+xml", CompressionLevel.Default);
Rel = relPart.CreateRelationship(UriHelper.GetRelativeUri(relPart.Uri, Uri), TargetMode.Internal, ExcelPackage.schemaRelationshipsSlicer);
var xml = new XmlDocument();
XmlHelper.LoadXmlSafe(xml, "<slicers xmlns:xr10=\"http://schemas.microsoft.com/office/spreadsheetml/2016/revision10\" xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" mc:Ignorable=\"x xr10\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\" />", Encoding.UTF8);
XmlDocument = xml;
}
else
{
Rel = relPart.GetRelationship(relId);
Uri = UriHelper.ResolvePartUri(relPart.Uri, Rel.TargetUri);
Part = relPart.Package.GetPart(Uri);
var xml = new XmlDocument();
XmlHelper.LoadXmlSafe(xml, Part.GetStream());
XmlDocument = xml;
}
}
internal ZipPackageRelationship Rel
{
get;
}
internal ZipPackagePart Part
{
get;
}
internal Uri Uri
{
get;
}
public XmlDocument XmlDocument
{
get;
}
}
}