-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cell.cs
executable file
·117 lines (109 loc) · 4.26 KB
/
Cell.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
116
117
/*----------------------------------------------------------------
// Copyright (C) 2015 广州,Lucky Game
//
// 模块名:
// 创建者:D.S.Qiu
// 修改者列表:
// 创建日期:June 03 2016
// 模块描述:
//----------------------------------------------------------------*/
using System;
using System.Text.RegularExpressions;
namespace PureExcel
{
/// <summary>
/// Contains the actual value
/// </summary>
public class Cell
{
//common escape char by xml
public const string LtEscape = "<";//<
public const string Lt = "<";
public const string GtEscape = ">";//>
public const string Gt = ">";
public const string AmpEscape = "&";//&
public const string Amp = "&";
public const string AposEscape = "'";//'
public const string Apos = "'";
public const string QuotEscape = """;//"
public const string Quot = "\"";
/// <summary>
/// Column Numnber (Starts at 1)
/// </summary>
public int ColumnIndex { get; set; }
/// <summary>
/// The value that is stored
/// </summary>
public string Value { get; set; }
/// <summary>
/// Create a new Cell
/// </summary>
/// <param name="cellElement">Cell</param>
/// <param name="sharedStrings">The collection of shared strings used by this document</param>
internal Cell(XMLNode cellElement, SharedStrings sharedStrings)
{
bool iShareString = cellElement.GetValue ("@t") == "s";
string columnName = cellElement.GetValue ("@r");
this.ColumnIndex = GetExcelColumnNumber(columnName);
this.Value = cellElement.GetValue("v>0>_text");
if (this.Value != null && iShareString)
{
this.Value = sharedStrings.GetString(this.Value);
}
}
public static string DecodeEscapeString(string cellValue)
{
cellValue = cellValue.Replace(LtEscape, Lt);
cellValue = cellValue.Replace(GtEscape, Gt);
cellValue = cellValue.Replace(AmpEscape, Amp);
cellValue = cellValue.Replace(AposEscape, Apos);
cellValue = cellValue.Replace(QuotEscape, Quot);
return cellValue;
}
//http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa
/// <summary>
/// Convert Column Number into Column Name - Character(s) eg 1-A, 2-B
/// </summary>
/// <param name="columnNumber">Column Number</param>
/// <returns>Column Name - Character(s)</returns>
public static string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = string.Concat(Convert.ToChar(65 + modulo), columnName);
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
//http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa
/// <summary>
/// Covert Column Name - Character(s) into a Column Number eg A-1, B-2, A1 - 1, B9 - 2
/// </summary>
/// <param name="columnName">Column Name - Character(s) optinally with the Row Number</param>
/// <param name="includesRowNumber">Specify if the row number is included</param>
/// <returns>Column Number</returns>
public static int GetExcelColumnNumber(string columnName, bool includesRowNumber = true)
{
if (includesRowNumber)
{
columnName = Regex.Replace(columnName, @"\d", "");
}
int[] digits = new int[columnName.Length];
for (int i = 0; i < columnName.Length; ++i)
{
digits[i] = Convert.ToInt32(columnName[i]) - 64;
}
int mul = 1; int res = 0;
for (int pos = digits.Length - 1; pos >= 0; --pos)
{
res += digits[pos] * mul;
mul *= 26;
}
return res;
}
}
}