-
Notifications
You must be signed in to change notification settings - Fork 11
/
SharedStringFunctions.cs
115 lines (104 loc) · 4.24 KB
/
SharedStringFunctions.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace SpreadsheetLight
{
public partial class SLDocument
{
/// <summary>
/// Get existing shared strings. WARNING: This is only a snapshot. Any changes made to the returned result are not used.
/// </summary>
/// <returns>A list of existing shared strings.</returns>
public List<SLRstType> GetSharedStrings()
{
List<SLRstType> result = new List<SLRstType>();
SLRstType rst = new SLRstType();
for (int i = 0; i < listSharedString.Count; ++i)
{
rst.FromHash(listSharedString[i]);
result.Add(rst.Clone());
}
return result;
}
/// <summary>
/// Get existing shared strings in a list of SharedStringItem objects. WARNING: This is only a snapshot. Any changes made to the returned result are not used.
/// </summary>
/// <returns>A list of existing SharedStringItem objects.</returns>
public List<SharedStringItem> GetSharedStringItems()
{
List<SharedStringItem> result = new List<SharedStringItem>();
SharedStringItem ssi;
for (int i = 0; i < listSharedString.Count; ++i)
{
ssi = new SharedStringItem();
ssi.InnerXml = listSharedString[i];
result.Add(ssi);
}
return result;
}
internal void LoadSharedStringTable()
{
countSharedString = 0;
listSharedString = new List<string>();
dictSharedStringHash = new Dictionary<string, int>();
if (wbp.SharedStringTablePart != null)
{
OpenXmlReader oxr = OpenXmlReader.Create(wbp.SharedStringTablePart);
while (oxr.Read())
{
if (oxr.ElementType == typeof(SharedStringItem))
{
this.ForceSaveToSharedStringTable((SharedStringItem)oxr.LoadCurrentElement());
}
}
oxr.Dispose();
countSharedString = listSharedString.Count;
}
}
internal void WriteSharedStringTable()
{
if (wbp.SharedStringTablePart != null)
{
if (listSharedString.Count > countSharedString)
{
wbp.SharedStringTablePart.SharedStringTable.Count = (uint)listSharedString.Count;
wbp.SharedStringTablePart.SharedStringTable.UniqueCount = (uint)dictSharedStringHash.Count;
int diff = listSharedString.Count - countSharedString;
for (int i = 0; i < diff; ++i)
{
wbp.SharedStringTablePart.SharedStringTable.Append(new SharedStringItem()
{
InnerXml = listSharedString[i + countSharedString]
});
}
wbp.SharedStringTablePart.SharedStringTable.Save();
}
}
else
{
if (listSharedString.Count > 0)
{
SharedStringTablePart sstp = wbp.AddNewPart<SharedStringTablePart>();
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(ms))
{
sw.Write("<x:sst count=\"{0}\" uniqueCount=\"{1}\" xmlns:x=\"{2}\">", listSharedString.Count, dictSharedStringHash.Count, SLConstants.NamespaceX);
for (int i = 0; i < listSharedString.Count; ++i)
{
sw.Write("<x:si>{0}</x:si>", listSharedString[i]);
}
sw.Write("</x:sst>");
sw.Flush();
ms.Position = 0;
sstp.FeedData(ms);
}
}
}
}
}
}
}