using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace ExcelZipLib
{
public class Cell
{
///
/// Used for converting from Excel column/row to column index starting at 0
///
[XmlAttribute("r")]
public string CellReference
{
get
{
return ColumnIndex.ToString();
}
set
{
ColumnIndex = GetColumnIndex(value);
if (ColumnIndex > worksheet.MaxColumnIndex)
worksheet.MaxColumnIndex = ColumnIndex;
}
}
///
/// The t type
///
[XmlAttribute("t")]
public string tType = "";
///
/// Original value of the Excel cell
///
[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);
}
}
}
///
/// Index of the orignal Excel cell column starting at 0
///
[XmlIgnore]
public int ColumnIndex;
///
/// Text of the Excel cell (if it was a string)
///
[XmlIgnore]
public string Text = "";
///
/// Amount of the Excel cell (if it was a number)
///
[XmlIgnore]
public double Amount;
///
/// The is amount
///
[XmlIgnore]
public bool IsAmount;
///
/// The _value.
///
private string _value = "";
///
/// Gets the index of the column.
///
/// The cell reference.
///
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;
}
}
}