diff --git a/README.md b/README.md index 4cb23ab..49fbdcc 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ At present, most popular frameworks need to load all the data into the memory to - Support `real-time` operation of each row of data ![miniexcel_lazy_load](https://user-images.githubusercontent.com/12729184/111034290-e5588a80-844f-11eb-8c84-6fdb6fb8f403.gif) - Support LINQ deferred execution, it can do low-consumption, fast paging and other complex queries -- Lightweight, without Microsoft Office installed, no COM+, third-party dependencies, DLL size is less than 100KB +- Lightweight, without Microsoft Office installed, no COM+, third-party dependencies, DLL size is less than 150KB - Easy API style to read/write/fill excel ### Get Started diff --git a/README.zh-CN.md b/README.zh-CN.md index 377fc3d..6e4adbf 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -32,7 +32,7 @@ MiniExcel简单、高效避免OOM的.NET处理Excel查、写、填充数据工 - 支持`即时`操作每行数据 ![miniexcel_lazy_load](https://user-images.githubusercontent.com/12729184/111034290-e5588a80-844f-11eb-8c84-6fdb6fb8f403.gif) - 兼具搭配 LINQ 延迟查询特性,能办到低消耗、快速分页等复杂查询 -- 轻量,不需要安装 Microsoft Office、COM+、不依赖任何套件,DLL小于100KB +- 轻量,不需要安装 Microsoft Office、COM+、不依赖任何套件,DLL小于150KB - 简便操作的 API 风格 diff --git a/README.zh-Hant.md b/README.zh-Hant.md index 8edbe07..f391e8b 100644 --- a/README.zh-Hant.md +++ b/README.zh-Hant.md @@ -29,7 +29,7 @@ MiniExcel 簡單、高效避免OOM的.NET處理Excel查、寫、填充工具。 - 兼具搭配 LINQ 延遲查詢特性,能辦到低消耗、快速分頁等複雜查詢 圖片:與主流框架對比的消耗、效率差 ![20210419](https://user-images.githubusercontent.com/12729184/114679083-6ef4c400-9d3e-11eb-9f78-a86daa45fe46.gif) -- 輕量,不需要安裝 Microsoft Office、COM+、不依賴任何套件,DLL小於100KB +- 輕量,不需要安裝 Microsoft Office、COM+、不依賴任何套件,DLL小於150KB - 簡便操作的 API 風格 diff --git a/docs/README.md b/docs/README.md index 4fbf355..6d75e5d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,9 @@ --- +### 0.17.2 +-[Bug] Fix v0.16.0-0.17.1 custom format contains specific format (eg:`#,##0.000_);[Red]\(#,##0.000\)`), automatic converter will convert double to datetime #267 + ### 0.17.1 - [New] Add QueryAsDataTableAsync(this Stream stream..) - [OPT] More clear strong type conversion error message [#I3X2ZL](https://gitee.com/dotnetchina/MiniExcel/issues/I3X2ZL) diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index f46c17e..507dcae 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -23,6 +23,9 @@ --- +### 0.17.2 +- [Bug] 修复 v0.16.0-0.17.1 自定义格式含有特定格式(e.g:`#,##0.000_);[Red]\(#,##0.000\)`),自动转换器会将 double 被转成 datetime 异常 #267 + ### 0.17.1 - [New] 增加 QueryAsDataTableAsync(this Stream stream..) - [OPT] 强型别 Query 转型错误信息能知道在哪一行列出错 [#I3X2ZL](https://gitee.com/dotnetchina/MiniExcel/issues/I3X2ZL) diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 0dff4d8..1c07172 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -17,6 +17,9 @@ --- +### 0.17.2 +- [Bug] 修復 v0.16.0-0.17.1 自定義格式含有特定格式(e.g:`#,##0.000_);[Red]\(#,##0.000\)`),自動轉換器會將 double 被轉成 datetime 異常 #267 + ### 0.17.1 - [New] 增加 QueryAsDataTableAsync(this Stream stream..) - [OPT] 強型別 Query 轉型錯誤訊息能知道在哪一行列出錯 [#I3X2ZL](https://gitee.com/dotnetchina/MiniExcel/issues/I3X2ZL) diff --git a/samples/xlsx/TestIssue267.xlsx b/samples/xlsx/TestIssue267.xlsx new file mode 100644 index 0000000..7c0ea7a Binary files /dev/null and b/samples/xlsx/TestIssue267.xlsx differ diff --git a/src/MiniExcel/MiniExcelLibs.csproj b/src/MiniExcel/MiniExcelLibs.csproj index a67071a..3da7cc6 100644 --- a/src/MiniExcel/MiniExcelLibs.csproj +++ b/src/MiniExcel/MiniExcelLibs.csproj @@ -1,34 +1,39 @@ - - net45;netstandard2.0;net5.0 - 0.17.1 - - - MiniExcel - MiniExcel - MiniExcel - excel;xlsx;micro-helper;mini;openxml;helper; - A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without third-party dependencies to create/query/template-fill-data. + + net45;netstandard2.0;net5.0 + 0.17.2 + + + MiniExcel + MiniExcel + MiniExcel + excel;xlsx;micro-helper;mini;openxml;helper; + + A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without third-party dependencies to create/query/template-fill-data. - Github : https://github.com/shps951023/MiniExcel - Gitee : https://gitee.com/dotnetchina/MiniExcel - Issues : https://github.com/shps951023/MiniExcel/issues - Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true - - ITWeiHan - ©2021 WeiHan Lin - https://raw.githubusercontent.com/shps951023/MiniExcel/master/LICENSE.md - https://raw.githubusercontent.com/shps951023/MiniExcel/master/LICENSE.md - https://github.com/shps951023/MiniExcel - https://github.com/shps951023/MiniExcel - https://user-images.githubusercontent.com/12729184/115023335-39440c80-9ef1-11eb-8771-7260d1e50d5a.png - Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs) - Github - - - - - - - + Github : https://github.com/shps951023/MiniExcel + Gitee : https://gitee.com/dotnetchina/MiniExcel + Issues : https://github.com/shps951023/MiniExcel/issues + Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true + + ITWeiHan + ©2021 WeiHan Lin + https://raw.githubusercontent.com/shps951023/MiniExcel/master/LICENSE.md + https://raw.githubusercontent.com/shps951023/MiniExcel/master/LICENSE.md + https://github.com/shps951023/MiniExcel + https://github.com/shps951023/MiniExcel + https://user-images.githubusercontent.com/12729184/115023335-39440c80-9ef1-11eb-8771-7260d1e50d5a.png + Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs) + Github + + + + + + + + + + + diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs index f702e7d..c063b67 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs @@ -78,7 +78,7 @@ //TODO: determine the type according to the format var type = typeof(string); - if(DateTimeHelper.isDateTimeFormat(formatCode)) + if(DateTimeHelper.IsDateTimeFormat(formatCode)) { type = typeof(DateTime?); } @@ -129,6 +129,11 @@ { if (double.TryParse(value?.ToString(), out var s)) { + //TODO: if format like yyyy, it need to convert to double, not return datetime value + + + + return DateTimeHelper.FromOADate(s); } } diff --git a/src/MiniExcel/Utils/DateTimeHelper.cs b/src/MiniExcel/Utils/DateTimeHelper.cs index d7dce6a..05a701e 100644 --- a/src/MiniExcel/Utils/DateTimeHelper.cs +++ b/src/MiniExcel/Utils/DateTimeHelper.cs @@ -1,17 +1,29 @@ namespace MiniExcelLibs.Utils { + using ExcelNumberFormat; using System; using System.Globalization; internal static partial class DateTimeHelper { private static DateTime basicDate = new DateTime(2021, 01, 01); - public static bool isDateTimeFormat(string formatCode) + + /// + /// NumberFormat from NuGet ExcelNumberFormat MIT@License + /// + public static bool IsDateTimeFormat(string formatCode) + { + return new NumberFormat(formatCode).IsDateTimeFormat; + } + + public static bool IsSingleDatetimeFormat(string formatCode) { var isDatetimeFormat = DateTime.TryParseExact(basicDate.ToString(formatCode), formatCode, CultureInfo.InvariantCulture, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite, out var date); - //Console.WriteLine($"format {formatCode} formatDate {date}"); if (basicDate != date) isDatetimeFormat = false; + + // TODO: double check + // TODO: datetime format like yyyy need to convert to string not datetime return isDatetimeFormat; } } diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 0002f1f..7b62f9a 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -26,6 +26,53 @@ namespace MiniExcelLibs.Tests this.output = output; } + /// + /// v0.16.0-0.17.1 custom format contains specific format (eg:`#,##0.000_);[Red]\(#,##0.000\)`), automatic converter will convert double to datetime #267 + /// + [Fact] + public void TestIssue267() + { + var path = PathHelper.GetSamplePath("/xlsx/TestIssue267.xlsx"); + var row = MiniExcel.Query(path).SingleOrDefault(); + Assert.Equal(10618, row.A); + Assert.Equal("2021-02-23", row.B); + Assert.Equal(43.199999999999996, row.C); + Assert.Equal(1.2, row.D); + Assert.Equal(new DateTime(2021, 7, 5), row.E); + Assert.Equal(new DateTime(2021, 7, 5,15,2,46), row.F); + } + + + [Fact] + public void TestIssue268_DateFormat() + { + Assert.True(IsDateFormatString("dd/mm/yyyy")); + Assert.True(IsDateFormatString("dd-mmm-yy")); + Assert.True(IsDateFormatString("dd-mmmm")); + Assert.True(IsDateFormatString("mmm-yy")); + Assert.True(IsDateFormatString("h:mm AM/PM")); + Assert.True(IsDateFormatString("h:mm:ss AM/PM")); + Assert.True(IsDateFormatString("hh:mm")); + Assert.True(IsDateFormatString("hh:mm:ss")); + Assert.True(IsDateFormatString("dd/mm/yyyy hh:mm")); + Assert.True(IsDateFormatString("mm:ss")); + Assert.True(IsDateFormatString("mm:ss.0")); + Assert.True(IsDateFormatString("[$-809]dd mmmm yyyy")); + Assert.False(IsDateFormatString("#,##0;[Red]-#,##0")); + Assert.False(IsDateFormatString(@"#,##0.000_);[Red]\(#,##0.000\)")); + Assert.False(IsDateFormatString("0_);[Red](0)")); + Assert.False(IsDateFormatString(@"0\h")); + Assert.False(IsDateFormatString("0\"h\"")); + Assert.False(IsDateFormatString("0%")); + Assert.False(IsDateFormatString("General")); + Assert.False(IsDateFormatString(@"_-* #,##0\ _P_t_s_-;\-* #,##0\ _P_t_s_-;_-* "" - ""??\ _P_t_s_-;_-@_- ")); + } + + private bool IsDateFormatString(string formatCode) + { + return MiniExcelLibs.Utils.DateTimeHelper.IsDateTimeFormat(formatCode); + } + [Fact] public void TestIssueI3X2ZL() {