-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
213 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System.Globalization; | ||
using System; | ||
using ArrayToExcel._internal; | ||
using System.Collections.Generic; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public class CellDefault(object? value) : ICellValue | ||
{ | ||
public void Apply(Cell cell, uint row) => Apply(cell, value); | ||
|
||
internal static void Apply(Cell cell, object? value) | ||
{ | ||
cell.CellValue = GetCellValue(value); | ||
cell.DataType = GetCellType(value); | ||
cell.StyleIndex = cell.DataType == CellValues.Date ? 2 : 4u; | ||
} | ||
|
||
internal static CellValue GetCellValue(object? value) | ||
{ | ||
if (value == null) return new(); | ||
|
||
var type = value.GetType(); | ||
|
||
if (type == typeof(bool)) | ||
return new((bool)value ? _boolVals[1] : _boolVals[0]); | ||
|
||
if (type == typeof(DateTime)) | ||
return new(((DateTime)value).ToString(_dateFormat, _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)); | ||
|
||
return new(NormCellText(value.ToString()!)); | ||
} | ||
|
||
|
||
internal static string NormCellText(string value) | ||
{ | ||
return RegularExpressions.InvalidXmlChars().Replace(value.Length > _maxCellText ? value.Substring(0, _maxCellText) : value, string.Empty); | ||
} | ||
|
||
internal static CellValues GetCellType(object? value) | ||
{ | ||
var type = value?.GetType() ?? typeof(object); | ||
|
||
if (type == typeof(bool)) | ||
return CellValues.Boolean; | ||
|
||
if (_numericTypes.Contains(type)) | ||
return CellValues.Number; | ||
|
||
if (type == typeof(DateTime) || type == typeof(DateTimeOffset)) | ||
return CellValues.Date; | ||
|
||
return CellValues.String; | ||
} | ||
|
||
static readonly HashSet<Type> _numericTypes = [ | ||
typeof(short), | ||
typeof(ushort), | ||
typeof(int), | ||
typeof(uint), | ||
typeof(long), | ||
typeof(ulong), | ||
typeof(double), | ||
typeof(decimal), | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public class CellFormula(Func<uint, string, string> cellText) : ICellValue | ||
{ | ||
public CellFormula(Func<uint, string> rowText) : this((row, cell) => rowText(row)) { } | ||
public CellFormula(string text) : this((row, col) => text) { } | ||
|
||
public void Apply(Cell cell, uint row) | ||
{ | ||
cell.CellFormula = new DocumentFormat.OpenXml.Spreadsheet.CellFormula(cellText(row, cell.CellReference!)); | ||
cell.StyleIndex = 4; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public class CellHyperlink(string link, string? text = null) : ICellValue | ||
{ | ||
public CellHyperlink(Uri link, string? text = null) | ||
: this(link.ToString(), text ?? link.OriginalString) { } | ||
|
||
public void Apply(Cell cell, uint row) | ||
{ | ||
cell.CellFormula = new DocumentFormat.OpenXml.Spreadsheet.CellFormula(_format.Value); | ||
cell.StyleIndex = 3; | ||
} | ||
|
||
readonly Lazy<string> _format = new(() => Format(link, text)); | ||
|
||
static string Format(string link, string? text) | ||
{ | ||
return string.Format(string.IsNullOrWhiteSpace(text) || text == link | ||
? "HYPERLINK(\"{0}\")" | ||
: "HYPERLINK(\"{0}\",\"{1}\")", | ||
Fix(link), Fix(text)); | ||
} | ||
|
||
static string? Fix(string? val) => val?.Replace("\"", "\"\""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public class CellPercent(object? value) : ICellValue | ||
{ | ||
public void Apply(Cell cell, uint row) | ||
{ | ||
cell.CellValue = CellDefault.GetCellValue(value); | ||
cell.DataType = CellDefault.GetCellType(value); | ||
cell.StyleIndex = 5; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using DocumentFormat.OpenXml; | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public class CellText(string? value, bool wrap = false) : ICellValue | ||
{ | ||
public void Apply(Cell cell, uint row) => Apply(cell, value, wrap); | ||
|
||
internal static void Apply(Cell cell, string? value, bool wrap) | ||
{ | ||
cell.InlineString = GetInlineString(value ?? string.Empty); | ||
cell.DataType = CellValues.InlineString; | ||
cell.StyleIndex = wrap ? 4 : 6u; | ||
} | ||
|
||
static InlineString GetInlineString(string value) | ||
{ | ||
return new InlineString(new DocumentFormat.OpenXml.Spreadsheet.Text(CellDefault.NormCellText(value)) | ||
{ | ||
Space = SpaceProcessingModeValues.Preserve | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using DocumentFormat.OpenXml.Spreadsheet; | ||
|
||
namespace ArrayToExcel; | ||
|
||
public interface ICellValue | ||
{ | ||
void Apply(Cell cell, uint row); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.