Skip to content

Commit

Permalink
2.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Feb 2, 2024
1 parent 48ecc99 commit 2a8e4d4
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 49 deletions.
6 changes: 3 additions & 3 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.4.2</AssemblyVersion>
<FileVersion>2.4.2</FileVersion>
<Version>2.4.2</Version>
<AssemblyVersion>2.4.3</AssemblyVersion>
<FileVersion>2.4.3</FileVersion>
<Version>2.4.3</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2024</Copyright>
Expand Down
41 changes: 41 additions & 0 deletions ArrayToExcel/CellDate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using DocumentFormat.OpenXml.Spreadsheet;
using System;

namespace ArrayToExcel;

public class CellDate : ICellValue
{
public CellDate(DateTime? dateTime, bool dateOnly = false)
{
_value = dateTime?.ToString(_dateTimeFormat);
_dateOnly = dateOnly;
}

public CellDate(DateTimeOffset? dateTime, bool dateOnly = false)
{
_value = dateTime?.ToString(_dateTimeFormat);
_dateOnly = dateOnly;
}

#if NET6_0_OR_GREATER
public CellDate(DateOnly? date)
{
_value = date?.ToString(_dateFormat);
_dateOnly = true;
}

static readonly string _dateFormat = "o";
#endif

static readonly string _dateTimeFormat = "s";
readonly string? _value;
readonly bool _dateOnly;

public virtual void Apply(Cell cell, uint row)
{

cell.CellValue = new(_value ?? string.Empty);
cell.DataType = CellValues.Date;
cell.StyleIndex = _dateOnly ? Styles.Date : Styles.DateTime;
}
}
37 changes: 13 additions & 24 deletions ArrayToExcel/CellDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,24 @@ internal static void Apply(Cell cell, object? value)
{
cell.CellValue = GetCellValue(value);
cell.DataType = GetCellType(value);
cell.StyleIndex = cell.DataType == CellValues.Date ? 2 : 4u;
cell.StyleIndex = Styles.Default;
}

static CellValue GetCellValue(object? value)
{
if (value == null) return new();

var type = value.GetType();
if (value is bool boolVal)
return new(_boolVals[boolVal ? 1 : 0]);

if (type == typeof(bool))
return new((bool)value ? _boolVals[1] : _boolVals[0]);
if (value is double doubleVal)
return new(doubleVal.ToString(_cultureInfo));

if (type == typeof(DateTime))
return new(((DateTime)value).ToString(_dateFormat, _cultureInfo));
if (value is decimal decimalVal)
return new(decimalVal.ToString(_cultureInfo));

if (type == typeof(DateTimeOffset))
return new(((DateTimeOffset)value).ToString(_dateFormat, _cultureInfo));

if (type == typeof(double))
return new(((double)value).ToString(_cultureInfo));

if (type == typeof(decimal))
return new(((decimal)value).ToString(_cultureInfo));

if (type == typeof(float))
return new(((float)value).ToString(_cultureInfo));
if (value is float floatVal)
return new(floatVal.ToString(_cultureInfo));

return new(NormCellText(value.ToString()!));
}
Expand All @@ -52,17 +44,15 @@ internal static string NormCellText(string value)

static CellValues GetCellType(object? value)
{
var type = value?.GetType() ?? typeof(object);
if (value == null)
return CellValues.String;

if (type == typeof(bool))
if (value is bool)
return CellValues.Boolean;

if (_numericTypes.Contains(type))
if (_numericTypes.Contains(value.GetType()))
return CellValues.Number;

if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
return CellValues.Date;

return CellValues.String;
}

Expand All @@ -78,7 +68,6 @@ static CellValues GetCellType(object? value)
typeof(float)];

static readonly CultureInfo _cultureInfo = CultureInfo.GetCultureInfo("en-US");
static readonly string _dateFormat = "s";
static readonly string[] _boolVals = ["0", "1"];

const int _maxCellText = 32767;
Expand Down
10 changes: 5 additions & 5 deletions ArrayToExcel/CellFormula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace ArrayToExcel;

public class CellFormula(Func<uint, string, string> cellText) : ICellValue
public class CellFormula(Func<uint, string, string> value, bool wrapText = false) : ICellValue
{
public CellFormula(Func<uint, string> rowText) : this((row, cell) => rowText(row)) { }
public CellFormula(string text) : this((row, col) => text) { }
public CellFormula(Func<uint, string> value, bool wrapText = false) : this((row, cell) => value(row), wrapText) { }
public CellFormula(string text, bool wrapText = false) : this((row, col) => text, wrapText) { }

public virtual void Apply(Cell cell, uint row)
{
cell.CellFormula = new DocumentFormat.OpenXml.Spreadsheet.CellFormula(cellText(row, cell.CellReference!));
cell.StyleIndex = 4;
cell.CellFormula = new DocumentFormat.OpenXml.Spreadsheet.CellFormula(value(row, cell.CellReference!));
cell.StyleIndex = wrapText ? Styles.WrapText : Styles.Default;
}
}
2 changes: 1 addition & 1 deletion ArrayToExcel/CellHyperlink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public CellHyperlink(Uri link, string? text = null)
public virtual void Apply(Cell cell, uint row)
{
cell.CellFormula = new DocumentFormat.OpenXml.Spreadsheet.CellFormula(_format.Value);
cell.StyleIndex = 3;
cell.StyleIndex = Styles.Hyperlink;
}

readonly Lazy<string> _format = new(() => Format(link, text));
Expand Down
2 changes: 1 addition & 1 deletion ArrayToExcel/CellPercent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class CellPercent(object? value) : CellDefault(value)
public override void Apply(Cell cell, uint row)
{
base.Apply(cell, row);
cell.StyleIndex = 5;
cell.StyleIndex = Styles.Percentage;
}
}
8 changes: 4 additions & 4 deletions ArrayToExcel/CellText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace ArrayToExcel;

public class CellText(string? value, bool wrap = false) : ICellValue
public class CellText(string? value, bool wrapText = false) : ICellValue
{
public virtual void Apply(Cell cell, uint row) => Apply(cell, value, wrap);
public virtual void Apply(Cell cell, uint row) => Apply(cell, value, wrapText);

internal static void Apply(Cell cell, string? value, bool wrap)
internal static void Apply(Cell cell, string? value, bool wrapText)
{
cell.InlineString = GetInlineString(value ?? string.Empty);
cell.DataType = CellValues.InlineString;
cell.StyleIndex = wrap ? 4 : 6u;
cell.StyleIndex = wrapText ? Styles.WrapText : Styles.Default;
}

static InlineString GetInlineString(string value)
Expand Down
24 changes: 17 additions & 7 deletions ArrayToExcel/ExcelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,19 @@ static void AddStyles(WorkbookPart workbookPart)
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat());
// header style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0, FontId = 1, BorderId = 0, FillId = 2, ApplyFill = true }).AppendChild(new Alignment { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center, WrapText = false });
// datetime style
// default style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat()).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top, WrapText = false });
// wraptext style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat()).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top, WrapText = true });
// date style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { ApplyNumberFormat = true, NumberFormatId = 14, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, ApplyFill = true }).AppendChild(new Alignment { Vertical = VerticalAlignmentValues.Top });
// datetime style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { ApplyNumberFormat = true, NumberFormatId = 22, FormatId = 0, FontId = 0, BorderId = 0, FillId = 0, ApplyFill = true }).AppendChild(new Alignment { Vertical = VerticalAlignmentValues.Top });
// hyperlink style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { FormatId = 0, FontId = 2 }).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top });
// multiline style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat()).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top, WrapText = true });
// percentage
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat { NumberFormatId = 3453 }).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top });
// nowrap style
stylesPart.Stylesheet.CellFormats.AppendChild(new CellFormat()).AppendChild(new Alignment() { Vertical = VerticalAlignmentValues.Top, WrapText = false });


stylesPart.Stylesheet.CellFormats.Count = (uint)stylesPart.Stylesheet.CellFormats.ChildElements.Count;

stylesPart.Stylesheet.Save();
Expand All @@ -164,7 +166,7 @@ static IEnumerable<Row> GetRows(IEnumerable items, List<ColumnSchema> columns, b
CellReference = GetColReference(i),
CellValue = new CellValue(x.Name),
DataType = CellValues.String,
StyleIndex = 1,
StyleIndex = Styles.Header,
}).ToArray();

var headerRow = new Row() { RowIndex = 1 };
Expand All @@ -189,6 +191,14 @@ static Cell GetCell(uint rowIndex, string? cellReference, object? value, bool wr
cellValue.Apply(cell, rowIndex);
else if (value is string str)
CellText.Apply(cell, str, wrapText);
else if (value is DateTime dateTime)
new CellDate(dateTime).Apply(cell, rowIndex);
else if (value is DateTimeOffset dateTimeOffset)
new CellDate(dateTimeOffset).Apply(cell, rowIndex);
#if NET6_0_OR_GREATER
else if (value is DateOnly dateOnly)
new CellDate(dateOnly).Apply(cell, rowIndex);
#endif
else if (value is Uri uri)
new CellHyperlink(uri).Apply(cell, rowIndex);
else
Expand Down
12 changes: 12 additions & 0 deletions ArrayToExcel/Styles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ArrayToExcel;

public static class Styles
{
public static readonly uint Header = 1;
public static readonly uint Default = 2;
public static readonly uint WrapText = 3;
public static readonly uint Date = 4;
public static readonly uint DateTime = 5;
public static readonly uint Hyperlink = 6;
public static readonly uint Percentage = 7;
}
7 changes: 4 additions & 3 deletions Examples/Example.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ static void TestTypes()
{
var items = Enumerable.Range(1, 100).Select(x => new
{
String = "1) text text text; \n2) text text text !!!",
WrapText = new CellText("1) text text text; \n2) text text text", wrap: true),
String = " 1) text text text; \n2) text text text !!!",
WrapText = new CellText("1) text text text; \n2) text text text", true),
Bool = x % 2 == 0,
NullableBool = x % 2 == 0 ? true : (bool?)null,
NullableBool = x % 2 == 0 ? (bool?)true : null,
Int = -x * 100,
Uint = (uint)x * 100,
Long = (long)x * 100,
Double = 1.1d + x,
Float = 1.1f + x,
Decimal = 1.1m + x,
DateOnly = DateOnly.FromDateTime(DateTime.Now.AddDays(-x)),
DateTime = DateTime.Now.AddDays(-x),
DateTimeOffset = DateTimeOffset.Now.AddDays(-x),
Uri = new Uri($"https://www.google.com/search?q={x}"),
Expand Down
2 changes: 1 addition & 1 deletion Examples/Example.ConsoleApp/SomeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ internal class SomeItem
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
public DateTime Prop3 { get; set; }
public DateTime? Prop3 { get; set; }
}
}

0 comments on commit 2a8e4d4

Please sign in to comment.