diff --git a/README-V2.md b/README-V2.md index d580431f..89229e2e 100644 --- a/README-V2.md +++ b/README-V2.md @@ -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 @@ -1221,7 +1233,7 @@ public class Dto } ``` -#### 5. System.ComponentModel.DisplayNameAttribute +#### 6. System.ComponentModel.DisplayNameAttribute The `DisplayNameAttribute` has the same effect as the `MiniExcelColumnNameAttribute`: @@ -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 @@ -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`: diff --git a/src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs b/src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs index fe055d6b..a49e964f 100644 --- a/src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs +++ b/src/MiniExcel.Core/Attributes/MiniExcelColumnAttribute.cs @@ -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; diff --git a/src/MiniExcel.Core/Attributes/MiniExcelHiddenAttribute.cs b/src/MiniExcel.Core/Attributes/MiniExcelHiddenAttribute.cs new file mode 100644 index 00000000..15436f12 --- /dev/null +++ b/src/MiniExcel.Core/Attributes/MiniExcelHiddenAttribute.cs @@ -0,0 +1,6 @@ +namespace MiniExcelLib.Core.Attributes; + +public class MiniExcelHiddenAttribute(bool hidden = true) : MiniExcelAttributeBase +{ + public bool Hidden { get; set; } = hidden; +} diff --git a/src/MiniExcel.Core/Reflection/ColumnMappingsProvider.cs b/src/MiniExcel.Core/Reflection/ColumnMappingsProvider.cs index acc2b0b0..1e0d72b5 100644 --- a/src/MiniExcel.Core/Reflection/ColumnMappingsProvider.cs +++ b/src/MiniExcel.Core/Reflection/ColumnMappingsProvider.cs @@ -143,6 +143,7 @@ internal static List GetMappingsForImport(Type type, str ExcelColumnIndex = m.GetAttribute()?.ExcelColumnIndex ?? excelColumnIndex, ExcelIndexName = m.GetAttribute()?.ExcelXName ?? excelColumn?.IndexName, ExcelColumnWidth = m.GetAttribute()?.ExcelColumnWidth ?? excelColumn?.Width, + ExcelHiddenColumn = m.GetAttribute()?.Hidden ?? excelColumn?.Hidden ?? false, ExcelFormat = excelFormat ?? excelColumn?.Format, ExcelFormatId = excelColumn?.FormatId ?? -1, ExcelColumnType = excelColumn?.Type ?? ColumnType.Value, @@ -201,9 +202,9 @@ private static void SetDictionaryColumnInfo(List 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; @@ -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; diff --git a/src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs b/src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs index c406bad9..47e26380 100644 --- a/src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs +++ b/src/MiniExcel.Core/Reflection/MiniExcelColumnMapping.cs @@ -14,7 +14,8 @@ public class MiniExcelColumnMapping 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? CustomFormatter { get; set; } diff --git a/src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs b/src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs index 6e99c376..a4bb8815 100644 --- a/src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs +++ b/src/MiniExcel.Core/WriteAdapters/DataReaderWriteAdapter.cs @@ -42,7 +42,7 @@ private IEnumerable GetRowValues(List pro 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()) diff --git a/src/MiniExcel.OpenXml/Constants/WorksheetXml.cs b/src/MiniExcel.OpenXml/Constants/WorksheetXml.cs index 0792dd0d..dd69f623 100644 --- a/src/MiniExcel.OpenXml/Constants/WorksheetXml.cs +++ b/src/MiniExcel.OpenXml/Constants/WorksheetXml.cs @@ -42,10 +42,10 @@ internal static string PaneSelection(string pane, string? activeCell, string? sq internal const string EndRow = ""; internal const string StartCols = ""; - internal static string Column(int colIndex, double columnWidth) - => $""""""; + internal static string Column(int colIndex, double columnWidth, bool hidden = false) + => $"""