Skip to content

Commit d110ea7

Browse files
committed
新增字体颜色和背景颜色功能(主要用于表格中某行某列需要高亮显示)&sample code &readme
1 parent 0cf3dab commit d110ea7

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/MiniWord/MiniExcel.Implment.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
210210
run.Append(hyperlink);
211211
t.Remove();
212212
}
213+
else if (tag.Value is MiniWordColorText)
214+
{
215+
var miniWordColorText = (MiniWordColorText)tag.Value;
216+
var colorText = AddColorText(miniWordColorText);
217+
run.Append(colorText);
218+
t.Remove();
219+
}
213220
else
214221
{
215222
var newText = string.Empty;
@@ -269,7 +276,19 @@ private static Hyperlink GetHyperLink(MainDocumentPart mainPart,MiniWorHyperLink
269276
};
270277
return xmlHyperLink;
271278
}
279+
private static RunProperties AddColorText(MiniWordColorText miniWordColorText)
280+
{
281+
282+
RunProperties runPro = new RunProperties();
283+
Text text = new Text(miniWordColorText.Text);
284+
Color color = new Color() { Val = miniWordColorText.ForeColor?.Replace("#", "") };
285+
Shading shading = new Shading() { Fill = miniWordColorText.BackColor?.Replace("#", "") };
286+
runPro.Append(shading);
287+
runPro.Append(color);
288+
runPro.Append(text);
272289

290+
return runPro;
291+
}
273292
private static void AddPicture(OpenXmlElement appendElement, string relationshipId, MiniWordPicture pic)
274293
{
275294
// Define the reference of the image.

src/MiniWord/MiniWordColorText.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniSoftware
8+
{
9+
public class MiniWordColorText
10+
{
11+
public string ForeColor { get; set; }
12+
public string Text { get; set; }
13+
public string BackColor { get; set; }
14+
}
15+
}

tests/MiniWordTests/IssueTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,24 @@ public void TestIssue4_new()
582582
};
583583
MiniWord.SaveAsByTemplate(path, templatePath, value);
584584
}
585+
[Fact]
586+
public void TestColor()
587+
{
588+
var path = PathHelper.GetTempFilePath();
589+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
590+
var value = new
591+
{
592+
Company_Name = new MiniWordColorText { Text = "MiniSofteware", ForeColor = "#eb70AB" },
593+
Name = new MiniWordColorText { Text = "Jack", BackColor = "#eb70AB" },
594+
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), BackColor = "#eb70AB", ForeColor = "#ffffff" },
595+
596+
VIP = true,
597+
Points = 123,
598+
APP = "Demo APP",
599+
};
600+
MiniWord.SaveAsByTemplate(path, templatePath, value);
601+
}
602+
585603

586604
#region Model:TestIssue18.docx
587605
public class Foo

0 commit comments

Comments
 (0)