Files
2025-03-13 12:04:22 -07:00

119 lines
2.8 KiB
C#

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace ExcelZipLib
{
public class Cell
{
/// <summary>
/// Used for converting from Excel column/row to column index starting at 0
/// </summary>
[XmlAttribute("r")]
public string CellReference
{
get
{
return ColumnIndex.ToString();
}
set
{
ColumnIndex = GetColumnIndex(value);
if (ColumnIndex > worksheet.MaxColumnIndex)
worksheet.MaxColumnIndex = ColumnIndex;
}
}
/// <summary>
/// The t type
/// </summary>
[XmlAttribute("t")]
public string tType = "";
/// <summary>
/// Original value of the Excel cell
/// </summary>
[XmlElement("v")]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
if (tType.Equals("s"))
{
Text = Workbook.SharedStrings.si[Convert.ToInt32(_value)].t;
return;
}
if (tType.Equals("str"))
{
Text = _value;
return;
}
try
{
Amount = Convert.ToDouble(_value, CultureInfo.InvariantCulture);
Text = Amount.ToString("#,##0.##");
IsAmount = true;
}
catch (Exception ex)
{
Amount = 0;
Text = String.Format("Cell Value '{0}': {1}", _value, ex.Message);
}
}
}
/// <summary>
/// Index of the orignal Excel cell column starting at 0
/// </summary>
[XmlIgnore]
public int ColumnIndex;
/// <summary>
/// Text of the Excel cell (if it was a string)
/// </summary>
[XmlIgnore]
public string Text = "";
/// <summary>
/// Amount of the Excel cell (if it was a number)
/// </summary>
[XmlIgnore]
public double Amount;
/// <summary>
/// The is amount
/// </summary>
[XmlIgnore]
public bool IsAmount;
/// <summary>
/// The _value.
/// </summary>
private string _value = "";
/// <summary>
/// Gets the index of the column.
/// </summary>
/// <param name="CellReference">The cell reference.</param>
/// <returns></returns>
private int GetColumnIndex(string CellReference)
{
string colLetter = new Regex("[A-Za-z]+").Match(CellReference).Value.ToUpper();
int colIndex = 0;
for (int i = 0; i < colLetter.Length; i++)
{
colIndex *= 26;
colIndex += (colLetter[i] - 'A' + 1);
}
return colIndex - 1;
}
}
}