Skip to content

Commit e4de9cd

Browse files
committed
support array list string to generate multiple row · Issue #11
1 parent e995ffc commit e4de9cd

File tree

8 files changed

+56
-9
lines changed

8 files changed

+56
-9
lines changed

samples/docx/DemoLogo.png

6.92 KB
Loading

samples/docx/TestBasicImage.docx

-565 Bytes
Binary file not shown.

samples/docx/TestDemo01.docx

34.1 KB
Binary file not shown.

samples/docx/TestDemo02.docx

27 KB
Binary file not shown.

samples/docx/TestIssue11.docx

12 KB
Binary file not shown.

samples/docx/demo01.png

85.6 KB
Loading

src/MiniWord/MiniWord.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using DocumentFormat.OpenXml.Packaging;
55
using DocumentFormat.OpenXml.Wordprocessing;
66
using System;
7+
using System.Collections;
78
using System.Collections.Generic;
89
using System.IO;
910
using System.Linq;
@@ -37,7 +38,25 @@ static void ReplaceTag(this OpenXmlElement xmlElement, WordprocessingDocument do
3738
var isMatch = t.Text.Contains($"{{{{{tag.Key}}}}}");
3839
if (isMatch)
3940
{
40-
if (tag.Value is MiniWordPicture)
41+
if (tag.Value is string[] || tag.Value is IList<string> || tag.Value is List<string>)
42+
{
43+
var vs = tag.Value as IEnumerable;
44+
var currentT = t;
45+
var isFirst = true;
46+
foreach (var v in vs)
47+
{
48+
var newT = t.CloneNode(false) as Text;
49+
if(isFirst)
50+
isFirst = false;
51+
else
52+
run.Append(new Break());
53+
run.Append(newT);
54+
currentT = newT;
55+
newT.Text = t.Text.Replace($"{{{{{tag.Key}}}}}", v?.ToString());
56+
}
57+
t.Remove();
58+
}
59+
else if (tag.Value is MiniWordPicture)
4160
{
4261
var pic = (MiniWordPicture)tag.Value;
4362
byte[] l_Data = null;

tests/MiniWordTests/IssueTests.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,41 @@ namespace MiniWordTests
1313
{
1414
public class IssueTests
1515
{
16+
/// <summary>
17+
/// [support array list string to generate multiple row · Issue #11 · mini-software/MiniWord]
18+
/// (https://github.com/mini-software/MiniWord/issues/11)
19+
/// </summary>
20+
[Fact]
21+
public void TestIssue11()
22+
{
23+
{
24+
var path = PathHelper.GetTempFilePath();
25+
var templatePath = PathHelper.GetFile("TestIssue11.docx");
26+
var value = new Dictionary<string, object>()
27+
{
28+
["managers"] = new[] { "Jack", "Alan" },
29+
["employees"] = new[] { "Mike", "Henry" },
30+
};
31+
MiniWord.SaveAsByTemplate(path, templatePath, value);
32+
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
33+
Assert.Contains("Jack", xml);
34+
}
35+
{
36+
var path = PathHelper.GetTempFilePath();
37+
var templatePath = PathHelper.GetFile("TestIssue11.docx");
38+
var value = new Dictionary<string, object>()
39+
{
40+
["managers"] = new List<string> { "Jack", "Alan" },
41+
["employees"] = new List<string> { "Mike", "Henry" },
42+
};
43+
MiniWord.SaveAsByTemplate(path, templatePath, value);
44+
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
45+
Assert.Contains("Jack", xml);
46+
}
47+
}
48+
49+
50+
1651
/// <summary>
1752
/// [Support image · Issue #3 · mini-software/MiniWord](https://github.com/mini-software/MiniWord/issues/3)
1853
/// </summary>
@@ -23,16 +58,9 @@ public void TestIssue3()
2358
var templatePath = PathHelper.GetFile("TestBasicImage.docx");
2459
var value = new Dictionary<string, object>()
2560
{
26-
["Name"] = "Jack",
27-
["Company_Name"] = "MiniSofteware",
28-
["CreateDate"] = new DateTime(2021, 01, 01),
29-
["VIP"] = true,
30-
["Points"] = 123,
31-
["APP"] = "Demo APP",
32-
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("TestBasicImage.png"), Width= 400, Height= 400 }
61+
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
3362
};
3463
MiniWord.SaveAsByTemplate(path, templatePath, value);
35-
//Console.WriteLine(path);
3664
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
3765
Assert.Contains("<w:drawing>", xml);
3866
}

0 commit comments

Comments
 (0)