From c7e4230729d4110f64e7c513a4c734f4a6fee9d8 Mon Sep 17 00:00:00 2001 From: wei Date: Mon, 10 May 2021 14:33:09 +0800 Subject: [PATCH] Fix SaveAsByTemplate single column demension index error #226 --- samples/xlsx/TestIssue226.xlsx | Bin 0 -> 9794 bytes .../OpenXml/ExcelOpenXmlTemplate.Impl.cs | 26 ++++++++++++------ tests/MiniExcelTests/MiniExcelIssueTests.cs | 14 ++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 samples/xlsx/TestIssue226.xlsx diff --git a/samples/xlsx/TestIssue226.xlsx b/samples/xlsx/TestIssue226.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..67290466d91a9a24c31427bb3f9044e658550200 GIT binary patch literal 9794 zcmeHNg#oeJmkz$47(wFw! z`+5%Ny!Q{>JNYt`e3`wz$?Ub|xArK>!NKDI5CKR40DuDEX`P7o8U_H!L;wID1CU^K z#KE>sX0}cSYHoIBj(RMvHrC|X@UZlm09fex|K0wB@4%f?;w%>dyBAmnUz+#u>6JmpN+(uG$S0qrp7-T(ml3A_vx;*R6f* zm3gWQBgp5^yavHq5M^ze@EWB`E^?ws7FNh)9q?1(f^p{_!Ce}r=FFJ%=7@h@(qN#J zlsg*7x*2Z*2d2ONkyPx6R3%$v86puHS!OiKS% zrn2kxXfO$ndOnw?Ris8hr$m7`|7lBS{b8W?<61v!N^#G4G^5}+EHai)t7$2xca$Wt z<;6~q2IE2@d>_Vv?~$$E&Jm!9|e?pw>S)BS<Mn zZ0Z>IiC)6dP}IcEX1#cd3$j{JgY*^OH9P?D;|Cl-=`Rslrw*d}4yBzuGyqYc5o+LI zX6?wz@^k;+$ovlu$3Ok`(&!h8-5^xRk@Rg)|Hbt3drUDo7g6aJ3N`PyGD{eB;W^Yq z%N=yYm}*b_;hy`ndEX8$E(?V3^;4XG<}3}x#^I-`cPWD;T-(0^KBaYtm$ENi@4<4O zKAS#IdM@im@7xyow4|{xOKxD5N^0_(L>1;Ji^h{jkMoEjc!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uSzw zFV^n}7zYm(@mixDmD!d__pKb9W}2c;a~5 zeAHz{(nVQQVN|h5)!-3ob6B>QU?L3h1D)L53={XxCvTNxo`nr(rFv}F-Q>>t`-aMy zkb(Rv?Lu7bHeot6_+xfhd%p61`v5wL;JHZ$^KPd|17DwkUsI>8S`y%#-bvMlzp&~` z3M{(b>p~H#4{|j}T8X!tSG!rDrgsBRM(0D^*RJSRgRHF{M<}(s-Q!%-opRozw6*GQ zeoZy#E-l!+!5Hg+`s5EL!@x2_^Z!3TA@gs~{rmV2&&w#u{T1M^1N?s*{yb(w!{blm z{D+1Qhnl}l`=AZigAwOL6N>P+(*Dr=ubKJJ<}b>EAZ~p@>*zHOH literal 0 HcmV?d00001 diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs index 6c26706..3f050e0 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs @@ -86,7 +86,7 @@ namespace MiniExcelLibs.OpenXml private List XRowInfos { get; set; } - private Dictionary XMergeCellInfos { get; set; } + private Dictionary XMergeCellInfos { get; set; } public List NewXMergeCellInfos { get; private set; } private void GenerateSheetXmlImpl(ZipArchiveEntry sheetZipEntry, Stream stream, Stream sheetStream, Dictionary inputMaps, List sharedStrings, XmlWriterSettings xmlWriterSettings = null) @@ -273,7 +273,7 @@ namespace MiniExcelLibs.OpenXml //TODO: ![image](https://user-images.githubusercontent.com/12729184/114848248-17735880-9e11-11eb-8258-63266bda0a1a.png) newRow.InnerXml = newRow.InnerXml.Replace(key, cellValueStr); } - } + } // note: only first time need add diff ![image](https://user-images.githubusercontent.com/12729184/114494728-6bceda80-9c4f-11eb-9685-8b5ed054eabe.png) if (!first) @@ -352,7 +352,7 @@ namespace MiniExcelLibs.OpenXml writer.Write($""); - if(this.NewXMergeCellInfos.Count != 0) + if (this.NewXMergeCellInfos.Count != 0) { writer.Write($"<{prefix}mergeCells count=\"{this.NewXMergeCellInfos.Count}\">"); foreach (var cell in this.NewXMergeCellInfos) @@ -465,7 +465,7 @@ namespace MiniExcelLibs.OpenXml { var first = true; //TODO:if CellIEnumerableValues is ICollection or Array then get length or Count - + foreach (var element in xRowInfo.CellIEnumerableValues) //TODO: optimize performance? { xRowInfo.CellIEnumerableValuesCount++; @@ -603,12 +603,20 @@ namespace MiniExcelLibs.OpenXml // e.g only need to update B6 to BMaxRowIndex var @refs = dimension.GetAttribute("ref").Split(':'); - var letter = new String(refs[1].Where(Char.IsLetter).ToArray()); - var digit = int.Parse(new String(refs[1].Where(Char.IsDigit).ToArray())); + if (@refs.Length == 2) + { + var letter = new String(refs[1].Where(Char.IsLetter).ToArray()); + var digit = int.Parse(new String(refs[1].Where(Char.IsDigit).ToArray())); - dimension.SetAttribute("ref", $"{refs[0]}:{letter}{digit + maxRowIndexDiff}"); + dimension.SetAttribute("ref", $"{refs[0]}:{letter}{digit + maxRowIndexDiff}"); + } + else + { + var letter = new String(refs[0].Where(Char.IsLetter).ToArray()); + var digit = int.Parse(new String(refs[0].Where(Char.IsDigit).ToArray())); + + dimension.SetAttribute("ref", $"A1:{letter}{digit + maxRowIndexDiff}"); + } } - - } } \ No newline at end of file diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 6c3ea0f..1ca9dba 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -25,6 +25,20 @@ namespace MiniExcelLibs.Tests this.output = output; } + + /// + /// https://github.com/shps951023/MiniExcel/issues/226 + /// Fix SaveAsByTemplate single column demension index error #226 + /// + [Fact] + public void Issue226() + { + var path = PathHelper.GetNewTemplateFilePath(); + var templatePath = PathHelper.GetSamplePath("xlsx/TestIssue226.xlsx"); + MiniExcel.SaveAsByTemplate(path, templatePath, new { employees = new[] { new { name = "123" }, new { name = "123" } } }); + Assert.Equal("A1:A3", Helpers.GetFirstSheetDimensionRefValue(path)); + } + /// /// ASP.NET Webform gridview datasource can't use miniexcel queryasdatatable ยท Issue #223] /// (https://github.com/shps951023/MiniExcel/issues/223)