Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

支持自定义字体颜色 #342

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

using System;

#if !NETSTANDARD2_0
using System.Drawing;
#endif

namespace Magicodes.ExporterAndImporter.Core
{

Expand Down Expand Up @@ -89,5 +93,10 @@ public ExporterHeaderAttribute(string displayName = null, float fontSize = 11, s
/// Hidden
/// </summary>
public bool Hidden { get; set; }

/// <summary>
/// 字体颜色
/// </summary>
public KnownColor FontColor { get; set; }
}
}
152 changes: 152 additions & 0 deletions src/Magicodes.ExporterAndImporter.Core/KnownColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@

#if NETSTANDARD2_0
namespace Magicodes.ExporterAndImporter.Core
{
public enum KnownColor
{
FirstColor = 0,
Transparent = 0,
AliceBlue = 1,
AntiqueWhite = 2,
Aqua = 3,
Aquamarine = 4,
Azure = 5,
Beige = 6,
Bisque = 7,
Black = 8,
BlanchedAlmond = 9,
Blue = 10,
BlueViolet = 11,
Brown = 12,
BurlyWood = 13,
CadetBlue = 14,
Chartreuse = 0xF,
Chocolate = 0x10,
Coral = 17,
CornflowerBlue = 18,
Cornsilk = 19,
Crimson = 20,
Cyan = 21,
DarkBlue = 22,
DarkCyan = 23,
DarkGoldenrod = 24,
DarkGray = 25,
DarkGreen = 26,
DarkKhaki = 27,
DarkMagenta = 28,
DarkOliveGreen = 29,
DarkOrange = 30,
DarkOrchid = 0x1F,
DarkRed = 0x20,
DarkSalmon = 33,
DarkSeaGreen = 34,
DarkSlateBlue = 35,
DarkSlateGray = 36,
DarkTurquoise = 37,
DarkViolet = 38,
DeepPink = 39,
DeepSkyBlue = 40,
DimGray = 41,
DodgerBlue = 42,
Firebrick = 43,
FloralWhite = 44,
ForestGreen = 45,
Fuchsia = 46,
Gainsboro = 47,
GhostWhite = 48,
Gold = 49,
Goldenrod = 50,
Gray = 51,
Green = 52,
GreenYellow = 53,
Honeydew = 54,
HotPink = 55,
IndianRed = 56,
Indigo = 57,
Ivory = 58,
Khaki = 59,
Lavender = 60,
LavenderBlush = 61,
LawnGreen = 62,
LemonChiffon = 0x3F,
LightBlue = 0x40,
LightCoral = 65,
LightCyan = 66,
LightGoldenrodYellow = 67,
LightGray = 68,
LightGreen = 69,
LightPink = 70,
LightSalmon = 71,
LightSeaGreen = 72,
LightSkyBlue = 73,
LightSlateGray = 74,
LightSteelBlue = 75,
LightYellow = 76,
Lime = 77,
LimeGreen = 78,
Linen = 79,
Magenta = 80,
Maroon = 81,
MediumAquamarine = 82,
MediumBlue = 83,
MediumOrchid = 84,
MediumPurple = 85,
MediumSeaGreen = 86,
MediumSlateBlue = 87,
MediumSpringGreen = 88,
MediumTurquoise = 89,
MediumVioletRed = 90,
MidnightBlue = 91,
MintCream = 92,
MistyRose = 93,
Moccasin = 94,
NavajoWhite = 95,
Navy = 96,
OldLace = 97,
Olive = 98,
OliveDrab = 99,
Orange = 100,
OrangeRed = 101,
Orchid = 102,
PaleGoldenrod = 103,
PaleGreen = 104,
PaleTurquoise = 105,
PaleVioletRed = 106,
PapayaWhip = 107,
PeachPuff = 108,
Peru = 109,
Pink = 110,
Plum = 111,
PowderBlue = 112,
Purple = 113,
Red = 114,
RosyBrown = 115,
RoyalBlue = 116,
SaddleBrown = 117,
Salmon = 118,
SandyBrown = 119,
SeaGreen = 120,
SeaShell = 121,
Sienna = 122,
Silver = 123,
SkyBlue = 124,
SlateBlue = 125,
SlateGray = 126,
Snow = 0x7F,
SpringGreen = 0x80,
SteelBlue = 129,
Tan = 130,
Teal = 131,
Thistle = 132,
Tomato = 133,
Turquoise = 134,
Violet = 135,
Wheat = 136,
White = 137,
WhiteSmoke = 138,
Yellow = 139,
YellowGreen = 140,
LastColor = 140
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
<Compile Remove="KnownColor.cs" />
</ItemGroup>
<ItemGroup>
<Compile Update="Resource.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,15 @@ protected virtual void AddStyle()
col.Style.WrapText = exporterHeader.ExporterHeaderAttribute.WrapText;
}
col.Hidden = exporterHeader.ExporterHeaderAttribute.Hidden;

if (exporterHeader.ExporterHeaderAttribute.FontColor != 0)
{
#if NETSTANDARD2_0
col.Style.Font.Color.SetColor(Color.FromName(exporterHeader.ExporterHeaderAttribute.FontColor.ToString()));
#else
col.Style.Font.Color.SetColor(Color.FromKnownColor(exporterHeader.ExporterHeaderAttribute.FontColor));
#endif
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,5 +960,20 @@ public async Task ValueMapping_Test()
sheet.Cells["C4"].Text.ShouldBe("");
}
}

[Fact(DisplayName = "单元格字体颜色设置数据导出测试")]
public async Task AttrExportWithColFontColorData_Test()
{
IExporter exporter = new ExcelExporter();

var filePath = GetTestFilePath($"{nameof(AttrExportWithColFontColorData_Test)}.xlsx");

DeleteFile(filePath);

var result = await exporter.Export(filePath, GenFu.GenFu.ListOf<ExportTestDataWithColFontColor>());

result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net461;net5.0</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net461;net5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Magicodes.ExporterAndImporter.Core;

namespace Magicodes.ExporterAndImporter.Tests.Models.Export
{
public class ExportTestDataWithColFontColor
{
[ExporterHeader(DisplayName = "姓名", IsBold = true, AutoCenterColumn = true)]
public string Name { get; set; }

#if !NETCOREAPP2_1
[ExporterHeader(DisplayName = "年龄", FontColor = System.Drawing.KnownColor.Red)]
#else
[ExporterHeader(DisplayName = "年龄", FontColor = KnownColor.Red)]
#endif
public int Age { get; set; }

}
}