Skip to content

Commit

Permalink
2.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Jan 8, 2024
1 parent bdaaf43 commit f69380d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 48 deletions.
15 changes: 4 additions & 11 deletions ArrayToExcel/ArrayToExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace ArrayToExcel
{
Expand Down Expand Up @@ -76,7 +75,7 @@ static MemoryStream CreateExcel(IEnumerable<SheetSchema> sheetSchemas)

static string NormSheetName(string? value, uint sheetId, HashSet<string> existNames)
{
value = _invalidSheetNameChars.Replace(value ?? string.Empty, string.Empty).Trim();
value = RegularExpressions.InvalidSheetNameChars().Replace(value ?? string.Empty, string.Empty).Trim();

if (string.IsNullOrWhiteSpace(value) || existNames.Contains(value))
return $"Sheet{sheetId}";
Expand Down Expand Up @@ -252,7 +251,7 @@ static InlineString GetInlineString(string value)

static string NormCellText(string value)
{
return _invalidXmlChars.Replace(value.Length > _maxCellText ? value.Substring(0, _maxCellText) : value, string.Empty);
return RegularExpressions.InvalidXmlChars().Replace(value.Length > _maxCellText ? value.Substring(0, _maxCellText) : value, string.Empty);
}

static CellValues GetCellType(object? value)
Expand Down Expand Up @@ -285,8 +284,7 @@ static string GetColReference(int index)
return new string(result.ToArray());
}

static readonly HashSet<Type> _numericTypes = new()
{
static readonly HashSet<Type> _numericTypes = [
typeof(short),
typeof(ushort),
typeof(int),
Expand All @@ -295,17 +293,12 @@ static string GetColReference(int index)
typeof(ulong),
typeof(double),
typeof(decimal),
typeof(float),
};
typeof(float)];

static readonly CultureInfo _cultureInfo = CultureInfo.GetCultureInfo("en-US");

static readonly string _digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

static readonly Regex _invalidXmlChars = new(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);

static readonly Regex _invalidSheetNameChars = new(@"[:?*\\/\[\]\r\n]|[\uDC00-\uDFFF]|[\uD800-\uDBFF]|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);

const int _maxSheetName = 31;

const int _maxCellText = 32767;
Expand Down
12 changes: 6 additions & 6 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.2.3</AssemblyVersion>
<FileVersion>2.2.3</FileVersion>
<Version>2.2.3</Version>
<AssemblyVersion>2.2.4</AssemblyVersion>
<FileVersion>2.2.4</FileVersion>
<Version>2.2.4</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2023</Copyright>
<Copyright>Leonid Salavatov 2024</Copyright>
<PackageId>ArrayToExcel</PackageId>
<Product>ArrayToExcel</Product>
<Title>ArrayToExcel</Title>
Expand All @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0" />
</ItemGroup>

</Project>
10 changes: 2 additions & 8 deletions ArrayToExcel/Formula.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

namespace ArrayToExcel
{
public class Formula
public class Formula(Func<uint, string> rowText)
{
public Formula(string text) : this(row => text) { }

public Formula(Func<uint, string> rowText)
{
RowText = rowText;
}

internal Func<uint, string> RowText { get; }

internal Func<uint, string> RowText { get; } = rowText;
}
}
21 changes: 21 additions & 0 deletions ArrayToExcel/RegularExpressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.RegularExpressions;

namespace ArrayToExcel
{
internal static partial class RegularExpressions
{
#if NET7_0_OR_GREATER
[GeneratedRegex(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled)]
public static partial Regex InvalidXmlChars();

[GeneratedRegex(@"[:?*\\/\[\]\r\n]|[\uDC00-\uDFFF]|[\uD800-\uDBFF]|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled)]
public static partial Regex InvalidSheetNameChars();
#else
static readonly Regex _invalidXmlChars = new(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);
public static Regex InvalidXmlChars() => _invalidXmlChars;

static readonly Regex _invalidSheetNameChars = new(@"[:?*\\/\[\]\r\n]|[\uDC00-\uDFFF]|[\uD800-\uDBFF]|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);
public static Regex InvalidSheetNameChars() => _invalidSheetNameChars;
#endif
}
}
15 changes: 4 additions & 11 deletions ArrayToExcel/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@

namespace ArrayToExcel
{
internal class SheetSchema
internal class SheetSchema(string sheetName, List<ColumnSchema> columns, IEnumerable items)
{
public SheetSchema(string sheetName, List<ColumnSchema> columns, IEnumerable items)
{
SheetName = sheetName;
Columns = columns;
Items = items;
}

public string SheetName { get; set; }
public List<ColumnSchema> Columns { get; set; }
public IEnumerable Items { get; set; }
public string SheetName { get; set; } = sheetName;
public List<ColumnSchema> Columns { get; set; } = columns;
public IEnumerable Items { get; set; } = items;
}

internal class ColumnSchema
Expand Down
8 changes: 4 additions & 4 deletions ArrayToExcel/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class SchemaBuilder<T>
{
internal SchemaBuilder(IEnumerable items, List<SheetSchema>? parentChilds = null)
{
Childs = parentChilds ?? new();
Childs = parentChilds ?? [];
Schema = new($"Sheet{(parentChilds?.Count + 2) ?? 1}", DefaultColumns(items), items);
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public SchemaBuilder<T> AddSheet<TList>(IEnumerable<TList> list, Action<SchemaBu
return this;
}

private List<ColumnSchema> DefaultColumns(IEnumerable items)
private static List<ColumnSchema> DefaultColumns(IEnumerable items)
{
var type = typeof(T);

Expand All @@ -103,7 +103,7 @@ private List<ColumnSchema> DefaultColumns(IEnumerable items)
var enumerator = items.GetEnumerator();

if (!enumerator.MoveNext())
return new List<ColumnSchema>();
return [];

type = enumerator.Current.GetType();
}
Expand All @@ -118,7 +118,7 @@ private List<ColumnSchema> DefaultColumns(IEnumerable items)
Name = kvp.Key,
Value = new(x => (x as IDictionary<string, object?>)?[kvp.Key]),
})
.ToList() ?? new List<ColumnSchema>();
.ToList() ?? [];
}

if (typeof(IDictionary).IsAssignableFrom(type))
Expand Down
2 changes: 1 addition & 1 deletion Examples/Example.ConsoleApp/Example.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
14 changes: 8 additions & 6 deletions Examples/Example.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
static void Main()
{
Example1();
Example2();
Expand All @@ -28,7 +28,7 @@ static void Main(string[] args)
TestTypes();
}

static IEnumerable<SomeItem> SomeItems = Enumerable.Range(1, 10).Select(x => new SomeItem
static readonly IEnumerable<SomeItem> SomeItems = Enumerable.Range(1, 10).Select(x => new SomeItem
{
Prop1 = $"Text #{x}",
Prop2 = x * 1000,
Expand Down Expand Up @@ -154,10 +154,12 @@ static void TestHashtable()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new Hashtable();
item.Add("Column #1", $"Text #{x}");
item.Add("Column #2", x * 1000);
item.Add("Column #3", DateTime.Now.AddDays(-x));
var item = new Hashtable
{
{ "Column #1", $"Text #{x}" },
{ "Column #2", x * 1000 },
{ "Column #3", DateTime.Now.AddDays(-x) }
};
return item;
});

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ Result:
![](https://raw.githubusercontent.com/mustaddon/ArrayToExcel/master/Examples/example6.png)


[Example.ConsoleApp](https://github.com/mustaddon/ArrayToExcel/tree/master/Examples/Example.ConsoleApp/)
[Example.ConsoleApp](https://github.com/mustaddon/ArrayToExcel/tree/master/Examples/Example.ConsoleApp/Program.cs)

0 comments on commit f69380d

Please sign in to comment.