diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLCell.cs
index 7561b9f..c6fc7a9 100644
--- a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLCell.cs
+++ b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLCell.cs
@@ -10,7 +10,6 @@
public interface IXLCell: IXLStylized
{
- Boolean Initialized { get; }
String Value { get; set; }
IXLAddress Address { get; }
XLCellValues DataType { get; set; }
diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLCell.cs
index 290db6c..dd39469 100644
--- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLCell.cs
+++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLCell.cs
@@ -14,9 +14,10 @@
Style = defaultStyle;
if (Style == null) Style = XLWorkbook.DefaultStyle;
}
- public Boolean Initialized { get; private set; }
+
public IXLAddress Address { get; private set; }
+ private Boolean initialized = false;
private String cellValue = String.Empty;
public String Value
{
@@ -28,27 +29,35 @@
{
String val = value;
- Double dTest;
- DateTime dtTest;
- Boolean bTest;
- if (Double.TryParse(val, out dTest))
+ if (!initialized)
{
- DataType = XLCellValues.Number;
- }
- else if (DateTime.TryParse(val, out dtTest))
- {
- DataType = XLCellValues.DateTime;
- Style.NumberFormat.NumberFormatId = 14;
- val = dtTest.ToOADate().ToString();
- }
- else if (Boolean.TryParse(val, out bTest))
- {
- DataType = XLCellValues.Boolean;
- val = bTest ? "1" : "0";
- }
- else
- {
- DataType = XLCellValues.SharedString;
+ Double dTest;
+ DateTime dtTest;
+ Boolean bTest;
+ if (val.Substring(0, 1) == "'")
+ {
+ val = val.Substring(1, val.Length - 1);
+ dataType = XLCellValues.SharedString;
+ }
+ else if (Double.TryParse(val, out dTest))
+ {
+ dataType = XLCellValues.Number;
+ }
+ else if (DateTime.TryParse(val, out dtTest))
+ {
+ dataType = XLCellValues.DateTime;
+ Style.NumberFormat.NumberFormatId = 14;
+ val = dtTest.ToOADate().ToString();
+ }
+ else if (Boolean.TryParse(val, out bTest))
+ {
+ dataType = XLCellValues.Boolean;
+ val = bTest ? "1" : "0";
+ }
+ else
+ {
+ dataType = XLCellValues.SharedString;
+ }
}
cellValue = val;
}
@@ -83,7 +92,64 @@
#endregion
+ private XLCellValues dataType;
+ public XLCellValues DataType
+ {
+ get
+ {
+ return dataType;
+ }
+ set
+ {
+ initialized = true;
+ if (value == XLCellValues.Boolean)
+ {
+ cellValue = Boolean.Parse(cellValue) ? "1" : "0";
+ }
+ else if (value == XLCellValues.DateTime)
+ {
+ DateTime dtTest;
+ Double dblTest;
+ if (DateTime.TryParse(cellValue, out dtTest))
+ {
+ cellValue = dtTest.ToOADate().ToString();
+ }
+ else if (Double.TryParse(cellValue, out dblTest))
+ {
+ cellValue = dblTest.ToString();
+ }
+ else
+ {
+ throw new ArgumentException("Cannot set data type to DateTime because '" + cellValue + "' is not recognized as a date.");
+ }
- public XLCellValues DataType { get; set; }
+ if (Style.NumberFormat.Format == String.Empty)
+ Style.NumberFormat.NumberFormatId = 14;
+ }
+ else if (value == XLCellValues.Number)
+ {
+ cellValue = Double.Parse(cellValue).ToString();
+ if (Style.NumberFormat.Format == String.Empty)
+ Style.NumberFormat.NumberFormatId = 0;
+ }
+ else
+ {
+ if (dataType == XLCellValues.Boolean)
+ {
+ cellValue = (cellValue == "0" ? false : true).ToString();
+ }
+ else if (dataType == XLCellValues.Number)
+ {
+ cellValue = Double.Parse(cellValue).ToString(Style.NumberFormat.Format);
+ }
+ else if (dataType == XLCellValues.DateTime)
+ {
+ cellValue = DateTime.FromOADate(Double.Parse(cellValue)).ToString(Style.NumberFormat.Format);
+ }
+ }
+
+ dataType = value;
+ }
+ }
}
}
diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj
index 34b9955..2af221b 100644
--- a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj
+++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj
@@ -53,6 +53,7 @@
+
diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/DataTypes.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/DataTypes.cs
new file mode 100644
index 0000000..1d98573
--- /dev/null
+++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/DataTypes.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using ClosedXML.Excel;
+using ClosedXML.Excel.Style;
+using System.Drawing;
+
+namespace ClosedXML_Examples.Misc
+{
+ public class DataTypes
+ {
+ #region Variables
+
+ // Public
+
+ // Private
+
+
+ #endregion
+
+ #region Properties
+
+ // Public
+
+ // Private
+
+ // Override
+
+
+ #endregion
+
+ #region Events
+
+ // Public
+
+ // Private
+
+ // Override
+
+
+ #endregion
+
+ #region Methods
+
+ // Public
+ public void Create(String filePath)
+ {
+ var workbook = new XLWorkbook();
+ var ws = workbook.Worksheets.Add("Data Types");
+
+ var co = 2;
+ var ro = 1;
+
+ ws.Cell(++ro, co).Value = "Plain Text:";
+ ws.Cell(ro, co + 1).Value = "Hello World.";
+
+ ws.Cell(++ro, co).Value = "Plain Date:";
+ ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2).ToString();
+
+ ws.Cell(++ro, co).Value = "Plain Boolean:";
+ ws.Cell(ro, co + 1).Value = true.ToString();
+
+ ws.Cell(++ro, co).Value = "Plain Number:";
+ ws.Cell(ro, co + 1).Value = "123.45";
+
+ ro++;
+
+ ws.Cell(++ro, co).Value = "Explicit Text:";
+ ws.Cell(ro, co + 1).Value = "'Hello World.";
+
+ ws.Cell(++ro, co).Value = "Date as Text:";
+ ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2).ToString();
+
+ ws.Cell(++ro, co).Value = "Boolean as Text:";
+ ws.Cell(ro, co + 1).Value = "'" + true.ToString();
+
+ ws.Cell(++ro, co).Value = "Number as Text:";
+ ws.Cell(ro, co + 1).Value = "'123.45";
+
+ ro++;
+
+ ws.Cell(++ro, co).Value = "Changing Data Types:";
+
+ ro++;
+
+ ws.Cell(++ro, co).Value = "Date to Text:";
+ ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2).ToString();
+ ws.Cell(ro, co + 1).DataType = XLCellValues.SharedString;
+
+ ws.Cell(++ro, co).Value = "Boolean to Text:";
+ ws.Cell(ro, co + 1).Value = true.ToString();
+ ws.Cell(ro, co + 1).DataType = XLCellValues.SharedString;
+
+ ws.Cell(++ro, co).Value = "Number to Text:";
+ ws.Cell(ro, co + 1).Value = "123.45";
+ ws.Cell(ro, co + 1).DataType = XLCellValues.SharedString;
+
+ ws.Cell(++ro, co).Value = "Text to Date:";
+ ws.Cell(ro, co + 1).Value = "'" + new DateTime(2010, 9, 2).ToString();
+ ws.Cell(ro, co + 1).DataType = XLCellValues.DateTime;
+
+ ws.Cell(++ro, co).Value = "Text to Boolean:";
+ ws.Cell(ro, co + 1).Value = "'" + true.ToString();
+ ws.Cell(ro, co + 1).DataType = XLCellValues.Boolean;
+
+ ws.Cell(++ro, co).Value = "Text to Number:";
+ ws.Cell(ro, co + 1).Value = "'123.45";
+ ws.Cell(ro, co + 1).DataType = XLCellValues.Number;
+
+ ro++;
+
+ ws.Cell(++ro, co).Value = "Formatted Date to Text:";
+ ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2).ToString();
+ ws.Cell(ro, co + 1).Style.NumberFormat.Format = "yyyy-MM-dd";
+ ws.Cell(ro, co + 1).DataType = XLCellValues.SharedString;
+
+ ws.Cell(++ro, co).Value = "Formatted Number to Text:";
+ ws.Cell(ro, co + 1).Value = "12345.6789";
+ ws.Cell(ro, co + 1).Style.NumberFormat.Format = "#,##0.00";
+ ws.Cell(ro, co + 1).DataType = XLCellValues.SharedString;
+
+ workbook.SaveAs(filePath);
+ }
+
+ // Private
+
+ // Override
+
+
+ #endregion
+ }
+}
diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs
index 1942617..fae14b5 100644
--- a/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs
+++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs
@@ -22,6 +22,7 @@
new InsertRows().Create(@"c:\InsertRows.xlsx");
new InsertColumns().Create(@"c:\InsertColumns.xlsx");
new ColumnCollection().Create(@"c:\ColumnCollection.xlsx");
+ new DataTypes().Create(@"c:\DataTypes.xlsx");
}
}
}
\ No newline at end of file