From ab3f27434baf1b822a6321b79be96c37b4b807b3 Mon Sep 17 00:00:00 2001 From: ITWeiHan Date: Sat, 19 Jun 2021 21:32:57 +0800 Subject: [PATCH] [New] SaveAsByTemplate support DateTime custom format #255 --- README.md | 2 +- README.zh-CN.md | 2 +- README.zh-Hant.md | 2 +- docs/README.md | 3 ++ docs/README.zh-CN.md | 3 ++ docs/README.zh-Hant.md | 3 ++ samples/xlsx/TestsIssue255_Template.xlsx | Bin 0 -> 5151 bytes .../OpenXml/ExcelOpenXmlTemplate.Impl.cs | 24 +++++++--- tests/MiniExcelTests/MiniExcelIssueTests.cs | 41 ++++++++++++++++++ 9 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 samples/xlsx/TestsIssue255_Template.xlsx diff --git a/README.md b/README.md index 2c98a06..86f0c9c 100644 --- a/README.md +++ b/README.md @@ -1182,7 +1182,7 @@ private IEnumerable> ConvertToIntIndexRows(IEnumerable> ConvertToIntIndexRows(IEnumerable> ConvertToIntIndexRows(IEnumerableF$t55fGFHNkO_hq>&Pdkv=p3 zf9{p*xySWA&&2cWwfDDIyzg2|TOAdR6bTa(6KTqJTo>t%;J|;IyMY`be7uOyiiBY` zQ~|<>LqBMo&x?;_3RX2Okt3NZet~HNH_slX^HX~G`GjB^mla{es`?)fK9tW`B(^?< zW+_9Jz~#nVQ88EdeF}2%VY}2V4u*pdh78o02`s;qPjoq+FeEr5zo+J(G12KTG812Y zT13+jVYO)G6Y6AYj(TggeHK}Ewq(0>8Ep^&3r(#m+f|Bqv2KQ<1Q=a4d>zLFV^`u6 zY9`VeR?zz^k4m%2k?+O1k0vJj6tH?F(lxtYza}cJ%|o&w!Yx~dWGnRD4Fph+sT6zD zoS?DOZ#(&El+9KKNn0HQ)4DvkZUugA2^1vw|Nmwf0{99~_!(d~2MaLRf!EW?FZ{yXi>_FsBw^WlS&WUFkT$ z*=7CpQnWH}^LZ(?HYvx4GC+deS*sK7_YMOjBXt?<;4*VazEMOK|7}QqzwUa~=y~vKI*t|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P_BEDzkk8;Io^#_fk{HyR-4My{Bh$8RQ#w;l%tMYEGg& z7u#m!cxt{YNOqULxTF} z%np_j-FRxCG~l6qyL>aI&txYqm<`LM#LKsPtweO}=HliJqZCJCW{NcL5gyZ1^w~I( z@^w!7`W0+7oe)9|p1kt#d~Ov@HC+-YxI5;{?y+H2vs#U9N%$>I_w<_@8H6!eD3GZM z;KpqHW8Ra%jR}+oJG+CN-OcsATtE<0ggsl5`ZYWG2^C@2&|V;E6DWvKZ45n@MgR5N z_?K)i?XKeXQ_c=4OSJ!Dsbyd?Fpz{Crl-#g6+29_a=DmMWhY|MCZf69g-&>A=4`&$ zVM!-1aBQt!AsLvIX4Aktg9@07=GB+<1oxnX8rwz^FJ$zN zV`>V5^3BIBymR&GR|KfOtMejCCUJJ+Qm5k|ut+-nMpJte=~C1!=dq&<~6Z!LgVW3ryY5f*9$qZyKbTsI4=RpPV%z9z9I}Ke* z`iw54jPpq_|AuaUsP}Fl&N33(?F1Ut zfvn`ch-t^~a(h0xrItQ>XTzNVG;-X&n^C7f1;u{)!E&2RHF>ClaDM!(TqYX57+wqa zoJNnV+|%i4jCk*ff$&b!`vF%|xMvK}5vYueLW=ZjmUDNQftcm|U%4~c`CajDFN>IU z{1Qs|QNK+-epkL*`y$%iU!n)+O}O%}=J$8iyX1-}j(^EhjQ>6NKT74_cezVc2qOL^ z;&3Y propInfo, object cellValue) + { + string cellValueStr; + //TODO:c.SetAttribute("t", "d"); and custom format + var efa = propInfo.Value.PropertyInfo.GetCustomAttribute(typeof(ExcelFormatAttribute)) as ExcelFormatAttribute; + if (efa == null) + cellValueStr = (cellValue as DateTime?)?.ToString("yyyy-MM-dd HH:mm:ss"); + else + cellValueStr = (cellValue as DateTime?)?.ToString(efa.Format); + return cellValueStr; + } + private static string CleanXml(string xml, string endPrefix) { //TODO: need to optimize diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 6d633fd..288eaa6 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -26,6 +26,47 @@ namespace MiniExcelLibs.Tests this.output = output; } + /// + /// [SaveAsByTemplate support DateTime custom format · Issue #255 · shps951023/MiniExcel] + /// (https://github.com/shps951023/MiniExcel/issues/255) + /// + [Fact] + public void Issue255() + { + //tempalte + { + var templatePath = PathHelper.GetSamplePath("xlsx/TestsIssue255_Template.xlsx"); + var path = PathHelper.GetTempPath(); + var value = new + { + Issue255DTO = new[] { + new Issue255DTO { Time = new DateTime(2021, 01, 01) } , + null + } + }; + MiniExcel.SaveAsByTemplate(path, templatePath, value); + var rows = MiniExcel.Query(path).ToList(); + Assert.Equal("2021", rows[1].A.ToString()); + } + //saveas + { + var path = PathHelper.GetTempPath(); + var value = new[] { + new Issue255DTO { Time = new DateTime(2021, 01, 01) } , + null + }; + MiniExcel.SaveAs(path, value); + var rows = MiniExcel.Query(path).ToList(); + Assert.Equal("2021", rows[1].A.ToString()); + } + } + + public class Issue255DTO + { + [ExcelFormat("yyyy")] + public DateTime Time { get; set; } + } + /// /// [Dynamic Query custom format not using mapping format · Issue #256] /// (https://github.com/shps951023/MiniExcel/issues/256)