-
Notifications
You must be signed in to change notification settings - Fork 1
/
SharedStrings.cs
executable file
·65 lines (61 loc) · 1.95 KB
/
SharedStrings.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
/*----------------------------------------------------------------
// Copyright (C) 2015 广州,Lucky Game
//
// 模块名:
// 创建者:D.S.Qiu
// 修改者列表:
// 创建日期:June 03 2016
// 模块描述:
//----------------------------------------------------------------*/
using System.Collections.Generic;
using System.IO.Compression;
namespace PureExcel
{
/// <summary>
/// Read and update xl/sharedStrings.xml file
/// </summary>
public class SharedStrings
{
//A dictionary is a lot faster than a list
private List<string> m_StringArray { get; set; }
private bool m_SharedStringsExist { get; set; }
internal SharedStrings(ZipArchive archive)
{
this.m_SharedStringsExist = true;
this.m_StringArray = new List<string> ();
XMLNode document = archive.GetXmlNode("xl/sharedStrings.xml");
if (document == null)
{
this.m_SharedStringsExist = false;
return;
}
//List<XMLNode> nodeList = new List<XMLNode> ();
//only one share string in one si!!!
XMLNodeList siNodeList = document.GetNodeList ("sst>0>si");
foreach (XMLNode node in siNodeList)
{
XMLNodeList tList = node.GetDeepNodeList("t");
//handle for <t xml:space="preserve"> </t>
string tValue = string.Empty;
foreach (var tNode in tList)
{
tValue += tNode.GetValue("_text");
}
this.m_StringArray.Add(tValue);
}
}
internal string GetString(string position)
{
if (!m_SharedStringsExist)
return null;
int pos = 0;
if (int.TryParse(position, out pos))
{
if (pos >= 0 && pos < this.m_StringArray.Count)
return m_StringArray[pos];
}
// TODO: should I throw an error? this is a corrupted excel document
return string.Empty;
}
}
}