Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ jobs:
git config user.email github-actions@github.com
git add ./*.md
git commit -am "Automated benchmark report - ${{ github.ref_name }}"
git push origin v1.x-maintenance --force-with-lease
git push origin HEAD:v1.x-maintenance --force-with-lease
92 changes: 45 additions & 47 deletions src/MiniExcel/Attributes/ExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
using MiniExcelLibs.Utils;
using System;

namespace MiniExcelLibs.Attributes
namespace MiniExcelLibs.Attributes;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnAttribute : Attribute
private int _index = -1;
private string _xName;

internal int FormatId { get; set; } = -1;

public string Name { get; set; }
public string[] Aliases { get; set; }
public double Width { get; set; } = 8.42857143;
public string Format { get; set; }
public bool Hidden { get; set; }
public bool Ignore { get; set; }
public ColumnType Type { get; set; } = ColumnType.Value;

public int Index
{
private int _index = -1;
private string _xName;

internal int FormatId { get; set; } = -1;

public string Name { get; set; }
public string[] Aliases { get; set; }
public double Width { get; set; } = 8.42857143;
public string Format { get; set; }
public bool Hidden { get; set; }
public bool Ignore { get; set; }
public ColumnType Type { get; set; } = ColumnType.Value;

public int Index
{
get => _index;
set => Init(value);
}

public string IndexName
{
get => _xName;
set => Init(ColumnHelper.GetColumnIndex(value), value);
}

private void Init(int index, string columnName = null)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Column index {index} must be greater or equal to zero.");

if (_xName == null)
_xName = columnName ?? ColumnHelper.GetAlphabetColumnName(index);

_index = index;
}
get => _index;
set => Init(value);
}

public enum ColumnType { Value, Formula }
public string IndexName
{
get => _xName;
set => Init(ColumnHelper.GetColumnIndex(value), value);
}

public class DynamicExcelColumn : ExcelColumnAttribute
private void Init(int index, string columnName = null)
{
public string Key { get; set; }
public Func<object, object> CustomFormatter { get; set; }
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), index, $"Column index {index} must be greater or equal to zero.");

public DynamicExcelColumn(string key)
{
Key = key;
}
if (_xName == null)
_xName = columnName ?? ColumnHelper.GetAlphabetColumnName(index);

_index = index;
}
}

public enum ColumnType { Value, Formula }

public class DynamicExcelColumn : ExcelColumnAttribute
{
public string Key { get; set; }
public Func<object, object> CustomFormatter { get; set; }

public DynamicExcelColumn(string key)
{
Key = key;
}
}
44 changes: 21 additions & 23 deletions src/MiniExcel/Attributes/ExcelColumnIndexAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
namespace MiniExcelLibs.Attributes
using MiniExcelLibs.Utils;

namespace MiniExcelLibs.Attributes;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnIndexAttribute : Attribute
{
using MiniExcelLibs.Utils;
using System;
public int ExcelColumnIndex { get; set; }
internal string ExcelXName { get; set; }
public ExcelColumnIndexAttribute(string columnName) => Init(ColumnHelper
.GetColumnIndex(columnName), columnName);
public ExcelColumnIndexAttribute(int columnIndex) => Init(columnIndex);

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnIndexAttribute : Attribute
private void Init(int columnIndex, string columnName = null)
{
public int ExcelColumnIndex { get; set; }
internal string ExcelXName { get; set; }
public ExcelColumnIndexAttribute(string columnName) => Init(ColumnHelper
.GetColumnIndex(columnName), columnName);
public ExcelColumnIndexAttribute(int columnIndex) => Init(columnIndex);

private void Init(int columnIndex, string columnName = null)
if (columnIndex < 0)
{
if (columnIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, $"Column index {columnIndex} must be greater or equal to zero.");
}
if (ExcelXName == null)
if (columnName != null)
ExcelXName = columnName;
else
ExcelXName = ColumnHelper.GetAlphabetColumnName(columnIndex);
ExcelColumnIndex = columnIndex;
throw new ArgumentOutOfRangeException(nameof(columnIndex), columnIndex, $"Column index {columnIndex} must be greater or equal to zero.");
}
if (ExcelXName == null)
if (columnName != null)
ExcelXName = columnName;
else
ExcelXName = ColumnHelper.GetAlphabetColumnName(columnIndex);
ExcelColumnIndex = columnIndex;
}
}
}
22 changes: 10 additions & 12 deletions src/MiniExcel/Attributes/ExcelColumnNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
namespace MiniExcelLibs.Attributes
namespace MiniExcelLibs.Attributes;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnNameAttribute : Attribute
{
using System;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnNameAttribute : Attribute
public string ExcelColumnName { get; set; }
public string[] Aliases { get; set; }
public ExcelColumnNameAttribute(string excelColumnName, string[] aliases = null)
{
public string ExcelColumnName { get; set; }
public string[] Aliases { get; set; }
public ExcelColumnNameAttribute(string excelColumnName, string[] aliases = null)
{
ExcelColumnName = excelColumnName;
Aliases = aliases;
}
ExcelColumnName = excelColumnName;
Aliases = aliases;
}
}
}
17 changes: 7 additions & 10 deletions src/MiniExcel/Attributes/ExcelColumnWidthAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
namespace MiniExcelLibs.Attributes
{
using System;
namespace MiniExcelLibs.Attributes;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnWidthAttribute : Attribute
{
public double ExcelColumnWidth { get; set; }
public ExcelColumnWidthAttribute(double excelColumnWidth) => ExcelColumnWidth = excelColumnWidth;
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnWidthAttribute : Attribute
{
public double ExcelColumnWidth { get; set; }
public ExcelColumnWidthAttribute(double excelColumnWidth) => ExcelColumnWidth = excelColumnWidth;
}
15 changes: 6 additions & 9 deletions src/MiniExcel/Attributes/ExcelFormatAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
namespace MiniExcelLibs.Attributes;

namespace MiniExcelLibs.Attributes
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelFormatAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelFormatAttribute : Attribute
{
public string Format { get; set; }
public ExcelFormatAttribute(string format) => Format = format;
}
}
public string Format { get; set; }
public ExcelFormatAttribute(string format) => Format = format;
}
15 changes: 6 additions & 9 deletions src/MiniExcel/Attributes/ExcelHiddenAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
namespace MiniExcelLibs.Attributes;

namespace MiniExcelLibs.Attributes
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ExcelHiddenAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class ExcelHiddenAttribute : Attribute
{
public bool ExcelHidden { get; set; }
public ExcelHiddenAttribute(bool excelHidden = true) => ExcelHidden = excelHidden;
}
}
public bool ExcelHidden { get; set; }
public ExcelHiddenAttribute(bool excelHidden = true) => ExcelHidden = excelHidden;
}
15 changes: 6 additions & 9 deletions src/MiniExcel/Attributes/ExcelIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
namespace MiniExcelLibs.Attributes;

namespace MiniExcelLibs.Attributes
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelIgnoreAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelIgnoreAttribute : Attribute
{
public bool ExcelIgnore { get; set; }
public ExcelIgnoreAttribute(bool excelIgnore = true) => ExcelIgnore = excelIgnore;
}
}
public bool ExcelIgnore { get; set; }
public ExcelIgnoreAttribute(bool excelIgnore = true) => ExcelIgnore = excelIgnore;
}
26 changes: 12 additions & 14 deletions src/MiniExcel/Attributes/ExcelSheetAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using MiniExcelLibs.OpenXml;
using System;

namespace MiniExcelLibs.Attributes
namespace MiniExcelLibs.Attributes;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExcelSheetAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExcelSheetAttribute : Attribute
{
public string Name { get; set; }
public SheetState State { get; set; } = SheetState.Visible;
}
public string Name { get; set; }
public SheetState State { get; set; } = SheetState.Visible;
}

public class DynamicExcelSheet : ExcelSheetAttribute
public class DynamicExcelSheet : ExcelSheetAttribute
{
public string Key { get; set; }
public DynamicExcelSheet(string key)
{
public string Key { get; set; }
public DynamicExcelSheet(string key)
{
Key = key;
}
Key = key;
}
}
34 changes: 15 additions & 19 deletions src/MiniExcel/Csv/CsvConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using System;
using System.IO;
using System.Text;

namespace MiniExcelLibs.Csv
{
public class CsvConfiguration : Configuration
{
private static readonly Encoding DefaultEncoding = new UTF8Encoding(true);
namespace MiniExcelLibs.Csv;

public char Seperator { get; set; } = ',';
public string NewLine { get; set; } = "\r\n";
public bool ReadLineBreaksWithinQuotes { get; set; } = true;
public bool ReadEmptyStringAsNull { get; set; } = false;
public bool AlwaysQuote { get; set; } = false;
public bool QuoteWhitespaces { get; set; } = true;
public Func<string, string[]> SplitFn { get; set; }
public Func<Stream, StreamReader> StreamReaderFunc { get; set; } = (stream) => new StreamReader(stream, DefaultEncoding);
public Func<Stream, StreamWriter> StreamWriterFunc { get; set; } = (stream) => new StreamWriter(stream, DefaultEncoding);
public class CsvConfiguration : Configuration
{
private static readonly Encoding DefaultEncoding = new UTF8Encoding(true);

internal static readonly CsvConfiguration DefaultConfiguration = new CsvConfiguration();
}
}
public char Seperator { get; set; } = ',';
public string NewLine { get; set; } = "\r\n";
public bool ReadLineBreaksWithinQuotes { get; set; } = true;
public bool ReadEmptyStringAsNull { get; set; } = false;
public bool AlwaysQuote { get; set; } = false;
public bool QuoteWhitespaces { get; set; } = true;
public Func<string, string[]> SplitFn { get; set; }
public Func<Stream, StreamReader> StreamReaderFunc { get; set; } = (stream) => new StreamReader(stream, DefaultEncoding);
public Func<Stream, StreamWriter> StreamWriterFunc { get; set; } = (stream) => new StreamWriter(stream, DefaultEncoding);

internal static readonly CsvConfiguration DefaultConfiguration = new CsvConfiguration();
}
39 changes: 19 additions & 20 deletions src/MiniExcel/Csv/CsvHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
namespace MiniExcelLibs.Csv
namespace MiniExcelLibs.Csv;

internal static class CsvHelpers
{
internal static class CsvHelpers
/// <summary>If content contains special characters then use "{value}" format</summary>
public static string ConvertToCsvValue(string value, CsvConfiguration configuration)
{
/// <summary>If content contains special characters then use "{value}" format</summary>
public static string ConvertToCsvValue(string value, CsvConfiguration configuration)
{
if (value == null)
return string.Empty;
if (value == null)
return string.Empty;

if (value.Contains("\""))
{
value = value.Replace("\"", "\"\"");
return $"\"{value}\"";
}
if (value.Contains("\""))
{
value = value.Replace("\"", "\"\"");
return $"\"{value}\"";
}

var shouldQuote = configuration.AlwaysQuote ||
(configuration.QuoteWhitespaces && value.Contains(" ")) ||
value.Contains(configuration.Seperator.ToString()) ||
value.Contains("\r") ||
value.Contains("\n");
var shouldQuote = configuration.AlwaysQuote ||
(configuration.QuoteWhitespaces && value.Contains(" ")) ||
value.Contains(configuration.Seperator.ToString()) ||
value.Contains("\r") ||
value.Contains("\n");

return shouldQuote ? $"\"{value}\"" : value;
}
return shouldQuote ? $"\"{value}\"" : value;
}
}
}
Loading
Loading