Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formula attribute added to support in rows with dto or dynamic attrib… #679

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions src/MiniExcel/Attributes/ExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ExcelColumnAttribute : Attribute

public bool Ignore { get; set; }

public bool Formula { get; set; } = false;
RaZer0k marked this conversation as resolved.
Show resolved Hide resolved

public int Index
{
get => _index;
Expand Down
4 changes: 2 additions & 2 deletions src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal static string Column(int? colIndex, double? columnWidth)
internal static string EmptyCell(string cellReference, string styleIndex)
=> $"<x:c r=\"{cellReference}\" s=\"{styleIndex}\"></x:c>";
//t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png)
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false)
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:v>{cellValue}</x:v></x:c>";
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, bool formula = false)
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:{(formula ? "f" : "v")}>{cellValue}</x:{(formula ? "f" : "v")}></x:c>";

internal static string Autofilter(string dimensionRef)
=> $"<x:autoFilter ref=\"{dimensionRef}\" />";
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde

/*Prefix and suffix blank space will lost after SaveAs #294*/
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: p.ExcelFormula));
}

private async Task GenerateEndXmlAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName

prop.Nullable = true;
prop.ExcelIgnore = dynamicColumn.Ignore;
prop.ExcelFormula = dynamicColumn.Formula;
prop.ExcelColumnIndex = dynamicColumn.Index;
prop.ExcelColumnWidth = dynamicColumn.Width;
//prop.ExcludeNullableType = item2[key]?.GetType();
Expand Down
3 changes: 2 additions & 1 deletion src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex

/*Prefix and suffix blank space will lost after SaveAs #294*/
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
var isFormula = columnInfo?.ExcelFormula ?? false;
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: isFormula));
}

private static void WriteCell(MiniExcelStreamWriter writer, string cellReference, string columnName)
Expand Down
5 changes: 4 additions & 1 deletion src/MiniExcel/Utils/CustomPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal class ExcelColumnInfo
public string ExcelIndexName { get; internal set; }
public bool ExcelIgnore { get; internal set; }
public int ExcelFormatId { get; internal set; }
public bool ExcelFormula { get; internal set; }
}

internal class ExcellSheetInfo
Expand Down Expand Up @@ -206,7 +207,8 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
ExcelIndexName = p.GetAttribute<ExcelColumnIndexAttribute>()?.ExcelXName ?? excelColumn?.IndexName,
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth ?? excelColumn?.Width,
ExcelFormat = excelFormat ?? excelColumn?.Format,
ExcelFormatId = excelColumn?.FormatId ?? -1
ExcelFormatId = excelColumn?.FormatId ?? -1,
ExcelFormula = excelColumn?.Formula ?? false
};
}).Where(_ => _ != null);
}
Expand Down Expand Up @@ -307,6 +309,7 @@ internal static void SetDictionaryColumnInfo(List<ExcelColumnInfo> _props, objec
p.ExcelColumnName = dynamicColumn.Name;
isIgnore = dynamicColumn.Ignore;
p.ExcelColumnWidth = dynamicColumn.Width;
p.ExcelFormula = dynamicColumn.Formula;
}
}
if (!isIgnore)
Expand Down