-
-
Notifications
You must be signed in to change notification settings - Fork 386
Upgrade to .NET 8.0 and refactor input value extraction #681
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -395,6 +395,7 @@ FodyWeavers.xsd | |
*.msp | ||
|
||
# JetBrains Rider | ||
.idea/ | ||
*.sln.iml | ||
|
||
/BenchmarkDotNet.Artifacts | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
namespace MiniExcelLibs.OpenXml.SaveByTemplate | ||
{ | ||
using MiniExcelLibs.Utils; | ||
using MiniExcelLibs.Zip; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
|
||
internal partial class ExcelOpenXmlTemplate : IExcelTemplate, IExcelTemplateAsync | ||
{ | ||
private static readonly XmlNamespaceManager _ns; | ||
private static readonly Regex _isExpressionRegex; | ||
|
||
static ExcelOpenXmlTemplate() | ||
{ | ||
_isExpressionRegex = new Regex("(?<={{).*?(?=}})"); | ||
_ns = new XmlNamespaceManager(new NameTable()); | ||
_ns.AddNamespace("x", Config.SpreadsheetmlXmlns); | ||
_ns.AddNamespace("x14ac", Config.SpreadsheetmlXml_x14ac); | ||
} | ||
|
||
private readonly Stream _stream; | ||
private readonly OpenXmlConfiguration _configuration; | ||
private readonly StringBuilder _calcChainContent = new StringBuilder(); | ||
|
||
public ExcelOpenXmlTemplate(Stream stream, IConfiguration configuration) | ||
{ | ||
_stream = stream; | ||
_configuration = (OpenXmlConfiguration)configuration ?? OpenXmlConfiguration.DefaultConfig; | ||
} | ||
|
||
public void SaveAsByTemplate(string templatePath, object value) | ||
{ | ||
using (var stream = FileHelper.OpenSharedRead(templatePath)) | ||
SaveAsByTemplateImpl(stream, value); | ||
} | ||
|
||
public void SaveAsByTemplate(byte[] templateBtyes, object value) | ||
{ | ||
using (Stream stream = new MemoryStream(templateBtyes)) | ||
SaveAsByTemplateImpl(stream, value); | ||
} | ||
|
||
public void SaveAsByTemplateImpl(Stream templateStream, object value) | ||
{ | ||
//only support xlsx | ||
templateStream.CopyTo(_stream); | ||
|
||
var reader = new ExcelOpenXmlSheetReader(_stream, null); | ||
var _archive = new ExcelOpenXmlZip(_stream, mode: ZipArchiveMode.Update, true, Encoding.UTF8); | ||
{ | ||
//read sharedString | ||
var sharedStrings = reader._sharedStrings; | ||
StringBuilder calcSheetContent = new StringBuilder(); | ||
|
||
//read all xlsx sheets | ||
var sheets = _archive.zipFile.Entries.Where(w => | ||
w.FullName.StartsWith("xl/worksheets/sheet", StringComparison.OrdinalIgnoreCase) | ||
|| w.FullName.StartsWith("/xl/worksheets/sheet", StringComparison.OrdinalIgnoreCase) | ||
).ToList(); | ||
|
||
int sheetIdx = 0; | ||
foreach (var sheet in sheets) | ||
{ | ||
this.XRowInfos = | ||
new List<XRowInfo>(); //every time need to use new XRowInfos or it'll cause duplicate problem: https://user-images.githubusercontent.com/12729184/115003101-0fcab700-9ed8-11eb-9151-ca4d7b86d59e.png | ||
this.XMergeCellInfos = new Dictionary<string, XMergeCell>(); | ||
this.NewXMergeCellInfos = new List<XMergeCell>(); | ||
|
||
var sheetStream = sheet.Open(); | ||
var fullName = sheet.FullName; | ||
|
||
ZipArchiveEntry entry = _archive.zipFile.CreateEntry(fullName); | ||
using (var zipStream = entry.Open()) | ||
{ | ||
GenerateSheetXmlImpl(sheet, zipStream, sheetStream, | ||
InputValueExtractor.ToValueDictionary(value), sharedStrings, false); | ||
//doc.Save(zipStream); //don't do it because :  | ||
} | ||
|
||
// disposing writer disposes streams as well. reopen the entry to read and parse calc functions | ||
using (var filledStream = entry.Open()) | ||
{ | ||
sheetIdx++; | ||
_calcChainContent.Append(CalcChainHelper.GetCalcChainContent(CalcChainCellRefs, sheetIdx)); | ||
} | ||
} | ||
|
||
var calcChain = _archive.zipFile.Entries.FirstOrDefault(e => e.FullName.Contains("xl/calcChain.xml")); | ||
if (calcChain != null) | ||
{ | ||
string calcChainPathname = calcChain.FullName; | ||
calcChain.Delete(); | ||
var calcChainEntry = _archive.zipFile.CreateEntry(calcChainPathname); | ||
using (var calcChainStream = calcChainEntry.Open()) | ||
{ | ||
CalcChainHelper.GenerateCalcChainSheet(calcChainStream, _calcChainContent.ToString()); | ||
} | ||
} | ||
} | ||
|
||
_archive.zipFile.Dispose(); | ||
} | ||
|
||
public Task SaveAsByTemplateAsync(string templatePath, object value, | ||
CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
return Task.Run(() => SaveAsByTemplate(templatePath, value), cancellationToken); | ||
} | ||
|
||
public Task SaveAsByTemplateAsync(byte[] templateBtyes, object value, | ||
CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
return Task.Run(() => SaveAsByTemplate(templateBtyes, value), cancellationToken); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Reflection; | ||
using MiniExcelLibs.Utils; | ||
|
||
namespace MiniExcelLibs.OpenXml.SaveByTemplate | ||
{ | ||
public static class InputValueExtractor | ||
{ | ||
public static IDictionary<string, object> ToValueDictionary(object valueObject) | ||
=> valueObject is Dictionary<string, object> valueDictionary | ||
? GetValuesFromDictionary(valueDictionary) | ||
: GetValuesFromObject(valueObject); | ||
|
||
private static IDictionary<string, object> GetValuesFromDictionary(Dictionary<string, object> valueDictionary) | ||
{ | ||
return valueDictionary | ||
.ToDictionary( | ||
x => x.Key, | ||
x => x.Value is IDataReader dataReader | ||
? TypeHelper.ConvertToEnumerableDictionary(dataReader).ToList() | ||
: x.Value); | ||
} | ||
|
||
private static IDictionary<string, object> GetValuesFromObject(object valueObject) | ||
{ | ||
var type = valueObject.GetType(); | ||
|
||
var propertyValues = type | ||
.GetProperties(BindingFlags.Public | BindingFlags.Instance) | ||
.Select(property => new { property.Name, Value = property.GetValue(valueObject) }); | ||
|
||
var fieldValues = type | ||
.GetFields(BindingFlags.Public | BindingFlags.Instance) | ||
.Select(field => new { field.Name, Value = field.GetValue(valueObject) }); | ||
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old code had a check to exclude a field by name if it was previously added as a property. I removed this check because I can't think how this can ever be true. But please correct me if I am wrong. |
||
|
||
return propertyValues | ||
.Concat(fieldValues) | ||
.ToDictionary(x => x.Name, x => x.Value); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.NET 6.0 (LTS) end date is Nov 12, 2024, we will follow the date to upgrade next month.