Skip to content

Commit fde6397

Browse files
Merge pull request #740 from michelebastione/features
- Resolves #592: The implementation of `DateTimeHelper.FromOADate` was equivalent to the standard `DateTime.FromOADate` already present in the framework. Although not being identical due to the latter using magic numbers instead of calculating some of the quantities, unwrapping those calculations showed a 1-1 match. - Resolves #605: Changed `using` statements to `await using` when compiling for .NET5+ so that `DisposeAsync` is called instead of `Dispose` - Closes #503 by clearing up the exception message
2 parents 05c8062 + 53e7be5 commit fde6397

13 files changed

+107
-205
lines changed

src/MiniExcel/Csv/CsvWriter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@ namespace MiniExcelLibs.Csv
1313
{
1414
internal class CsvWriter : IExcelWriter, IDisposable
1515
{
16-
private readonly Stream _stream;
16+
private readonly StreamWriter _writer;
1717
private readonly CsvConfiguration _configuration;
1818
private readonly bool _printHeader;
19-
private object _value;
20-
private readonly StreamWriter _writer;
19+
private readonly object _value;
2120
private bool _disposedValue;
2221

2322
public CsvWriter(Stream stream, object value, IConfiguration configuration, bool printHeader)
2423
{
25-
_stream = stream;
2624
_configuration = configuration == null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration;
2725
_printHeader = printHeader;
2826
_value = value;
29-
_writer = _configuration.StreamWriterFunc(_stream);
27+
_writer = _configuration.StreamWriterFunc(stream);
3028
}
3129

3230
public int[] SaveAs()

src/MiniExcel/ExcelFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static IExcelReader GetProvider(Stream stream, ExcelType excelType, ICo
1717
case ExcelType.XLSX:
1818
return new ExcelOpenXmlSheetReader(stream, configuration);
1919
default:
20-
throw new NotSupportedException("Please Issue for me");
20+
throw new NotSupportedException("Something went wrong. Please report this issue you are experiencing with MiniExcel.");
2121
}
2222
}
2323
}
@@ -55,7 +55,7 @@ internal static IExcelTemplateAsync GetProvider(Stream stream, IConfiguration co
5555
var valueExtractor = new InputValueExtractor();
5656
return new ExcelOpenXmlTemplate(stream, configuration, valueExtractor);
5757
default:
58-
throw new NotSupportedException("Please Issue for me");
58+
throw new NotSupportedException("Something went wrong. Please report this issue you are experiencing with MiniExcel.");
5959
}
6060
}
6161
}

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,14 @@ private async Task InsertContentTypesXmlAsync(CancellationToken cancellationToke
527527
await GenerateContentTypesXmlAsync(cancellationToken);
528528
return;
529529
}
530-
530+
#if NET5_0_OR_GREATER
531+
await using (var stream = contentTypesZipEntry.Open())
532+
#else
531533
using (var stream = contentTypesZipEntry.Open())
534+
#endif
532535
{
533536
var doc = XDocument.Load(stream);
534-
var ns = doc.Root.GetDefaultNamespace();
537+
var ns = doc.Root?.GetDefaultNamespace();
535538
var typesElement = doc.Descendants(ns + "Types").Single();
536539

537540
var partNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
@@ -562,9 +565,14 @@ private async Task CreateZipEntryAsync(string path, string contentType, string c
562565
cancellationToken.ThrowIfCancellationRequested();
563566

564567
var entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
568+
569+
#if NET5_0_OR_GREATER
570+
await using (var zipStream = entry.Open())
571+
#else
565572
using (var zipStream = entry.Open())
566-
using (var writer = new MiniExcelAsyncStreamWriter(zipStream, _utf8WithBom, _configuration.BufferSize, cancellationToken))
567-
await writer.WriteAsync(content);
573+
#endif
574+
using (var writer = new MiniExcelAsyncStreamWriter(zipStream, _utf8WithBom, _configuration.BufferSize, cancellationToken))
575+
await writer.WriteAsync(content);
568576

569577
if (!string.IsNullOrEmpty(contentType))
570578
_zipDictionary.Add(path, new ZipPackageInfo(entry, contentType));
@@ -575,7 +583,12 @@ private async Task CreateZipEntryAsync(string path, byte[] content, Cancellation
575583
cancellationToken.ThrowIfCancellationRequested();
576584

577585
var entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
586+
587+
#if NET5_0_OR_GREATER
588+
await using (var zipStream = entry.Open())
589+
#else
578590
using (var zipStream = entry.Open())
591+
#endif
579592
await zipStream.WriteAsync(content, 0, content.Length, cancellationToken);
580593
}
581594
}

src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace MiniExcelLibs.OpenXml
2-
{
3-
using MiniExcelLibs.Utils;
4-
using MiniExcelLibs.Zip;
5-
using System;
6-
using System.Collections.Generic;
1+
using MiniExcelLibs.Utils;
2+
using MiniExcelLibs.Zip;
3+
using System;
4+
using System.Collections.Generic;
75

6+
namespace MiniExcelLibs.OpenXml
7+
{
88
internal class ExcelOpenXmlStyles
99
{
1010
private static readonly string[] _ns = { Config.SpreadsheetmlXmlns, Config.SpreadsheetmlXmlStrictns };
@@ -14,68 +14,68 @@ internal class ExcelOpenXmlStyles
1414

1515
public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
1616
{
17-
using (var Reader = zip.GetXmlReader(@"xl/styles.xml"))
17+
using (var reader = zip.GetXmlReader(@"xl/styles.xml"))
1818
{
19-
if (!XmlReaderHelper.IsStartElement(Reader, "styleSheet", _ns))
19+
if (!XmlReaderHelper.IsStartElement(reader, "styleSheet", _ns))
2020
return;
21-
if (!XmlReaderHelper.ReadFirstContent(Reader))
21+
if (!XmlReaderHelper.ReadFirstContent(reader))
2222
return;
23-
while (!Reader.EOF)
23+
24+
while (!reader.EOF)
2425
{
25-
if (XmlReaderHelper.IsStartElement(Reader, "cellXfs", _ns))
26+
if (XmlReaderHelper.IsStartElement(reader, "cellXfs", _ns))
2627
{
27-
if (!XmlReaderHelper.ReadFirstContent(Reader))
28+
if (!XmlReaderHelper.ReadFirstContent(reader))
2829
continue;
2930

3031
var index = 0;
31-
while (!Reader.EOF)
32+
while (!reader.EOF)
3233
{
33-
if (XmlReaderHelper.IsStartElement(Reader, "xf", _ns))
34+
if (XmlReaderHelper.IsStartElement(reader, "xf", _ns))
3435
{
35-
int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
36-
int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
36+
int.TryParse(reader.GetAttribute("xfId"), out var xfId);
37+
int.TryParse(reader.GetAttribute("numFmtId"), out var numFmtId);
3738
_cellXfs.Add(index, new StyleRecord() { XfId = xfId, NumFmtId = numFmtId });
38-
Reader.Skip();
39+
reader.Skip();
3940
index++;
4041
}
41-
else if (!XmlReaderHelper.SkipContent(Reader))
42+
else if (!XmlReaderHelper.SkipContent(reader))
4243
break;
4344
}
4445
}
45-
else if (XmlReaderHelper.IsStartElement(Reader, "cellStyleXfs", _ns))
46+
else if (XmlReaderHelper.IsStartElement(reader, "cellStyleXfs", _ns))
4647
{
47-
if (!XmlReaderHelper.ReadFirstContent(Reader))
48+
if (!XmlReaderHelper.ReadFirstContent(reader))
4849
continue;
4950

5051
var index = 0;
51-
while (!Reader.EOF)
52+
while (!reader.EOF)
5253
{
53-
if (XmlReaderHelper.IsStartElement(Reader, "xf", _ns))
54+
if (XmlReaderHelper.IsStartElement(reader, "xf", _ns))
5455
{
55-
int.TryParse(Reader.GetAttribute("xfId"), out var xfId);
56-
int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
56+
int.TryParse(reader.GetAttribute("xfId"), out var xfId);
57+
int.TryParse(reader.GetAttribute("numFmtId"), out var numFmtId);
5758

5859
_cellStyleXfs.Add(index, new StyleRecord() { XfId = xfId, NumFmtId = numFmtId });
59-
Reader.Skip();
60+
reader.Skip();
6061
index++;
6162
}
62-
else if (!XmlReaderHelper.SkipContent(Reader))
63+
else if (!XmlReaderHelper.SkipContent(reader))
6364
break;
6465
}
6566
}
66-
else if (XmlReaderHelper.IsStartElement(Reader, "numFmts", _ns))
67+
else if (XmlReaderHelper.IsStartElement(reader, "numFmts", _ns))
6768
{
68-
if (!XmlReaderHelper.ReadFirstContent(Reader))
69+
if (!XmlReaderHelper.ReadFirstContent(reader))
6970
continue;
7071

71-
while (!Reader.EOF)
72+
while (!reader.EOF)
7273
{
73-
if (XmlReaderHelper.IsStartElement(Reader, "numFmt", _ns))
74+
if (XmlReaderHelper.IsStartElement(reader, "numFmt", _ns))
7475
{
75-
int.TryParse(Reader.GetAttribute("numFmtId"), out var numFmtId);
76-
var formatCode = Reader.GetAttribute("formatCode");
77-
78-
76+
int.TryParse(reader.GetAttribute("numFmtId"), out var numFmtId);
77+
var formatCode = reader.GetAttribute("formatCode");
78+
7979
//TODO: determine the type according to the format
8080
var type = typeof(string);
8181
if (DateTimeHelper.IsDateTimeFormat(formatCode))
@@ -84,15 +84,15 @@ public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
8484
}
8585

8686
_customFormats.Add(numFmtId, new NumberFormatString(formatCode, type));
87-
Reader.Skip();
87+
reader.Skip();
8888
}
89-
else if (!XmlReaderHelper.SkipContent(Reader))
89+
else if (!XmlReaderHelper.SkipContent(reader))
9090
{
9191
break;
9292
}
9393
}
9494
}
95-
else if (!XmlReaderHelper.SkipContent(Reader))
95+
else if (!XmlReaderHelper.SkipContent(reader))
9696
{
9797
break;
9898
}
@@ -102,52 +102,41 @@ public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
102102

103103
public NumberFormatString GetStyleFormat(int index)
104104
{
105-
if (_cellXfs.TryGetValue(index, out var styleRecord))
106-
{
107-
if (Formats.TryGetValue(styleRecord.NumFmtId, out var numberFormat))
108-
{
109-
return numberFormat;
110-
}
111-
else if (_customFormats.TryGetValue(styleRecord.NumFmtId, out var customNumberFormat))
112-
{
113-
return customNumberFormat;
114-
}
105+
if (!_cellXfs.TryGetValue(index, out var styleRecord))
115106
return null;
116-
}
107+
108+
if (Formats.TryGetValue(styleRecord.NumFmtId, out var numberFormat))
109+
return numberFormat;
110+
111+
if (_customFormats.TryGetValue(styleRecord.NumFmtId, out var customNumberFormat))
112+
return customNumberFormat;
113+
117114
return null;
118115
}
119116

120117
public object ConvertValueByStyleFormat(int index, object value)
121118
{
122-
var sf = this.GetStyleFormat(index);
123-
if (sf == null)
124-
return value;
125-
if (sf.Type == null)
119+
var sf = GetStyleFormat(index);
120+
if (sf?.Type == null)
126121
return value;
127122

128123
if (sf.Type == typeof(DateTime?))
129124
{
130125
if (double.TryParse(value?.ToString(), out var s))
131126
{
132-
if (s >= DateTimeHelper.OADateMaxAsDouble || s <= DateTimeHelper.OADateMinAsDouble)
133-
{
134-
return value;
135-
}
136-
return DateTimeHelper.FromOADate(s);
127+
return DateTimeHelper.IsValidOADateTime(s) ? DateTime.FromOADate(s) : value;
137128
}
138129
}
139130
else if (sf.Type == typeof(TimeSpan?))
140131
{
141132
if (double.TryParse(value?.ToString(), out var number))
142-
{
143133
return TimeSpan.FromDays(number);
144-
}
145134
}
146135

147136
return value;
148137
}
149138

150-
private static Dictionary<int, NumberFormatString> Formats { get; } = new Dictionary<int, NumberFormatString>()
139+
private static Dictionary<int, NumberFormatString> Formats { get; } = new Dictionary<int, NumberFormatString>
151140
{
152141
{ 0, new NumberFormatString("General",typeof(string)) },
153142
{ 1, new NumberFormatString("0",typeof(decimal?)) },

src/MiniExcel/Utils/CustomPropertyHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,8 @@ internal static bool TryGetTypeColumnInfo(Type type, Configuration configuration
303303
return false;
304304
}
305305

306-
if (type.IsValueType)
307-
throw new NotImplementedException($"MiniExcel does not support only {type.Name} value generic type");
308-
if (type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid))
309-
throw new NotImplementedException($"MiniExcel does not support only {type.Name} generic type");
306+
if (type.IsValueType || type == typeof(string))
307+
throw new NotSupportedException($"MiniExcel does not support the use of {type.FullName} as a generic type");
310308

311309
if (ValueIsNeededToDetermineProperties(type))
312310
{

0 commit comments

Comments
 (0)