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
27 changes: 21 additions & 6 deletions README-V2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,19 @@ public class Dto
}
```

#### 4. Multiple column names mapping to the same property.
#### 4. Set Column Visibility

```csharp
public class Dto
{
public string Name { get; set; }

[MiniExcelHidden]
public int SecretPoints { get; set; }
}
```

#### 5. Multiple column names mapping to the same property.

```csharp
public class Dto
Expand All @@ -1221,7 +1233,7 @@ public class Dto
}
```

#### 5. System.ComponentModel.DisplayNameAttribute
#### 6. System.ComponentModel.DisplayNameAttribute

The `DisplayNameAttribute` has the same effect as the `MiniExcelColumnNameAttribute`:

Expand All @@ -1240,22 +1252,25 @@ public class Dto
}
```

#### 6. MiniExcelColumnAttribute
#### 7. MiniExcelColumnAttribute

Multiple attributes can be simplified using the `MiniExcelColumnAttribute`:

```csharp
public class Dto
{
[MiniExcelColumn(Name = "ID",Index =0)]
[MiniExcelColumn(Name = "ID", Index = 0)]
public string MyProperty { get; set; }

[MiniExcelColumn(Name = "CreateDate", Index = 1, Format = "yyyy-MM", Width = 100)]
public DateTime MyProperty2 { get; set; }

[MiniExcelColumn(Name = "SecretColumn", Hidden = true)]
public int MyProperty3 { get; set; }
}
```

#### 7. DynamicColumnAttribute
#### 8. DynamicColumnAttribute

Attributes can also be set on columns dynamically:
```csharp
Expand All @@ -1276,7 +1291,7 @@ var exporter = MiniExcel.Exporters.GetOpenXmlExporter();
exporter.Export(path, value, configuration: config);
```

#### 8. MiniExcelSheetAttribute
#### 9. MiniExcelSheetAttribute

It is possible to define the name and visibility of a sheet through the `MiniExcelSheetAttribute`:

Expand Down
1 change: 1 addition & 0 deletions src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class MiniExcelColumnAttribute : MiniExcelAttributeBase
public string? Name { get; set; }
public string[]? Aliases { get; set; } = [];
public string? Format { get; set; }
public bool Hidden { get; set; }
public bool Ignore { get; set; }

internal int FormatId { get; private set; } = -1;
Expand Down
6 changes: 6 additions & 0 deletions src/MiniExcel.Core/Attributes/MiniExcelHiddenAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MiniExcelLib.Core.Attributes;

public class MiniExcelHiddenAttribute(bool hidden = true) : MiniExcelAttributeBase
{
public bool Hidden { get; set; } = hidden;
}
6 changes: 4 additions & 2 deletions src/MiniExcel.Core/Reflection/ColumnMappingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ internal static List<MiniExcelColumnMapping> GetMappingsForImport(Type type, str
ExcelColumnIndex = m.GetAttribute<MiniExcelColumnIndexAttribute>()?.ExcelColumnIndex ?? excelColumnIndex,
ExcelIndexName = m.GetAttribute<MiniExcelColumnIndexAttribute>()?.ExcelXName ?? excelColumn?.IndexName,
ExcelColumnWidth = m.GetAttribute<MiniExcelColumnWidthAttribute>()?.ExcelColumnWidth ?? excelColumn?.Width,
ExcelHiddenColumn = m.GetAttribute<MiniExcelHiddenAttribute>()?.Hidden ?? excelColumn?.Hidden ?? false,
ExcelFormat = excelFormat ?? excelColumn?.Format,
ExcelFormatId = excelColumn?.FormatId ?? -1,
ExcelColumnType = excelColumn?.Type ?? ColumnType.Value,
Expand Down Expand Up @@ -201,9 +202,9 @@ private static void SetDictionaryColumnInfo(List<MiniExcelColumnMapping?> props,
if (dynamicColumn.Name is { } colName)
mapping.ExcelColumnName = colName;


mapping.ExcelColumnIndex = dynamicColumn.Index;
mapping.ExcelColumnWidth = dynamicColumn.Width;
mapping.ExcelHiddenColumn = dynamicColumn.Hidden;
mapping.ExcelColumnType = dynamicColumn.Type;
mapping.CustomFormatter = dynamicColumn.CustomFormatter;

Expand Down Expand Up @@ -263,7 +264,8 @@ internal static MiniExcelColumnMapping GetColumnMappingFromDynamicConfiguration(
return member;

member.Nullable = true;
member.ExcelIgnore = dynamicColumn.Ignore;
member.ExcelIgnoreColumn = dynamicColumn.Ignore;
member.ExcelHiddenColumn = dynamicColumn.Hidden;
member.ExcelColumnType = dynamicColumn.Type;
member.ExcelColumnWidth = dynamicColumn.Width;
member.CustomFormatter = dynamicColumn.CustomFormatter;
Expand Down
3 changes: 2 additions & 1 deletion src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class MiniExcelColumnMapping
{
public object Key { get; set; }

Check warning on line 7 in src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Key' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public MiniExcelMemberAccessor MemberAccessor { get; set; }
public Type ExcludeNullableType { get; set; }
public bool Nullable { get; internal set; }
Expand All @@ -14,7 +14,8 @@
public string? ExcelFormat { get; internal set; }
public double? ExcelColumnWidth { get; internal set; }
public string? ExcelIndexName { get; internal set; }
public bool ExcelIgnore { get; internal set; }
public bool ExcelHiddenColumn { get; internal set; }
public bool ExcelIgnoreColumn { get; internal set; }
public int ExcelFormatId { get; internal set; }
public ColumnType ExcelColumnType { get; internal set; }
public Func<object?, object?>? CustomFormatter { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
var columnName = _reader.GetName(i);
if (!_configuration.DynamicColumnFirst ||
_configuration.DynamicColumns.Any(d => string.Equals(d.Key, columnName, StringComparison.OrdinalIgnoreCase)))

Check warning on line 21 in src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference argument for parameter 'source' in 'bool Enumerable.Any<DynamicExcelColumn>(IEnumerable<DynamicExcelColumn> source, Func<DynamicExcelColumn, bool> predicate)'.
{
var prop = ColumnMappingsProvider.GetColumnMappingFromDynamicConfiguration(columnName, _configuration);
props.Add(prop);
Expand All @@ -42,10 +42,10 @@
for (int i = 0; i < _reader.FieldCount; i++)
{
var prop = props[i];
if (prop is { ExcelIgnore: false })
if (prop is { ExcelIgnoreColumn: false })
{
var columnIndex = _configuration.DynamicColumnFirst
? _reader.GetOrdinal(prop.Key.ToString())

Check warning on line 48 in src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Possible null reference argument for parameter 'name' in 'int IDataRecord.GetOrdinal(string name)'.
: i;

yield return new CellWriteInfo(_reader.GetValue(columnIndex), column, prop);
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel.OpenXml/Constants/WorksheetXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ internal static string PaneSelection(string pane, string? activeCell, string? sq
internal const string EndRow = "</x:row>";
internal const string StartCols = "<x:cols>";

internal static string Column(int colIndex, double columnWidth)
=> $"""<x:col min="{colIndex}" max="{colIndex}" width="{columnWidth.ToString(CultureInfo.InvariantCulture)}" customWidth="1" />""";
internal static string Column(int colIndex, double columnWidth, bool hidden = false)
=> $"""<x:col min="{colIndex}" max="{colIndex}" width="{columnWidth.ToString(CultureInfo.InvariantCulture)}" hidden="{(hidden ? 1 : 0)}" customWidth="1" />""";

private static readonly int MaxColumnLength = Column(int.MaxValue, double.MaxValue).Length;
private static readonly int MaxColumnLength = Column(int.MaxValue, double.MaxValue, true).Length;

public static int GetColumnPlaceholderLength(int columnCount) => StartCols.Length + MaxColumnLength * columnCount + EndCols.Length;

Expand Down
10 changes: 6 additions & 4 deletions src/MiniExcel.OpenXml/Models/ExcelColumnWidth.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace MiniExcelLib.OpenXml.Models;

public sealed class ExcelColumnWidth(int index, double width)
public sealed class ExcelColumnWidth(int index, double width, bool hidden = false)
{
// Aptos is the default font for Office 2023 and onwards, over which the width of cells are calculated at the size of 11pt.
// Priorly it was Calibri, which had very similar parameters, so no visual differences should be noticed.
Expand All @@ -11,6 +11,7 @@ public sealed class ExcelColumnWidth(int index, double width)

public int Index { get; } = index;
public double Width { get; set; } = width;
public bool Hidden { get; set; } = hidden;

public static double GetWidthFromTextLength(double characters)
=> Math.Round(characters + Aptos11Padding, 8);
Expand All @@ -37,12 +38,13 @@ internal static ExcelColumnWidthCollection GetFromMappings(ICollection<MiniExcel

foreach (var map in mappings)
{
if (map?.ExcelColumnWidth is not null || minWidth is not null)
if (map is { ExcelHiddenColumn: true } or { ExcelColumnWidth: not null } || minWidth is not null)
{
var colIndex = map?.ExcelColumnIndex + 1 ?? i;
var width = map?.ExcelColumnWidth ?? minWidth!.Value;
var hidden = map?.ExcelHiddenColumn ?? false;
var width = map?.ExcelColumnWidth ?? minWidth ?? OpenXmlConfiguration.Default.MinWidth;

columnWidths.Add(new ExcelColumnWidth(colIndex, width + ExcelColumnWidth.Aptos11Padding));
columnWidths.Add(new ExcelColumnWidth(colIndex, width + ExcelColumnWidth.Aptos11Padding, hidden));
}

i++;
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel.OpenXml/OpenXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private async Task<int> WriteValuesAsync(EnhancedStreamWriter writer, object val
}

int maxRowIndex;
var maxColumnIndex = props.Count(x => x is { ExcelIgnore: false });
var maxColumnIndex = props.Count(x => x is { ExcelIgnoreColumn: false });

await writer.WriteAsync(WorksheetXml.StartWorksheetWithRelationship, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -412,7 +412,7 @@ private static async Task WriteColumnsWidthsAsync(EnhancedStreamWriter writer, I
await writer.WriteAsync(WorksheetXml.StartCols, cancellationToken).ConfigureAwait(false);
hasWrittenStart = true;
}
await writer.WriteAsync(WorksheetXml.Column(column.Index, column.Width), cancellationToken).ConfigureAwait(false);
await writer.WriteAsync(WorksheetXml.Column(column.Index, column.Width, column.Hidden), cancellationToken).ConfigureAwait(false);
}

if (!hasWrittenStart)
Expand All @@ -433,7 +433,7 @@ private async Task PrintHeaderAsync(EnhancedStreamWriter writer, List<MiniExcelC
//reason : https://github.com/mini-software/MiniExcel/issues/142
if (p is not null)
{
if (p.ExcelIgnore)
if (p.ExcelIgnoreColumn)
continue;

var r = CellReferenceConverter.GetCellFromCoordinates(xIndex, yIndex);
Expand Down
Loading