C# 将多种颜色应用于EPPlus中的单个单元格?
我正在使用EPPlus生成报告文件。有一列不用于任何筛选/排序逻辑,但包含关于行主题的逐行数据 客户希望它是由不同的颜色填充,如所附图片所示 我已经找到了类似的问题和正确的答案,但这些答案只适用于整个单元格需要用单色填充的情况C# 将多种颜色应用于EPPlus中的单个单元格?,c#,excel,openxml,epplus,C#,Excel,Openxml,Epplus,我正在使用EPPlus生成报告文件。有一列不用于任何筛选/排序逻辑,但包含关于行主题的逐行数据 客户希望它是由不同的颜色填充,如所附图片所示 我已经找到了类似的问题和正确的答案,但这些答案只适用于整个单元格需要用单色填充的情况 我想知道是否有一种方法可以通过EPPlus以编程方式实现我的案例,或者实现这种功能的唯一方法是通过OpenXML库手动修改单元格的元数据,或者甚至通过修改底层XML文件来修改单元格的元数据?创建一个名为MultipleColorsNacell的控制台应用程序,然后复制并
我想知道是否有一种方法可以通过EPPlus以编程方式实现我的案例,或者实现这种功能的唯一方法是通过OpenXML库手动修改单元格的元数据,或者甚至通过修改底层XML文件来修改单元格的元数据?创建一个名为MultipleColorsNacell的控制台应用程序,然后复制并粘贴下面的代码
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Data;
using System.IO;
namespace MultipleColorsInACell
{
class Program
{
static void Main(string[] args)
{
try
{
WorkbookPart workbookPart = null;
try
{
using (var memoryStream = new MemoryStream())
{
using (var excel = SpreadsheetDocument.Create(memoryStream, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook, true))
{
workbookPart = excel.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
uint sheetId = 1;
excel.WorkbookPart.Workbook.Sheets = new Sheets();
Sheets sheets = excel.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = "rId1";
WorksheetPart wSheetPart = workbookPart.AddNewPart<WorksheetPart>(relationshipId);
string sheetName = "MultipleColorsInACell";
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
Worksheet worksheet = new Worksheet();
wSheetPart.Worksheet = worksheet;
SheetData sheetData = new SheetData();
worksheet.Append(sheetData);
string[] excelColumns = new string[] { "A", "B", "C", "D", "E", "F", "G" };
Row row1 = new Row() { RowIndex = (UInt32Value)5U, Spans = new ListValue<StringValue>() { InnerText = "9:9" }, DyDescent = 0.3D };
Cell cell1 = new Cell() { CellReference = "I5", DataType = CellValues.SharedString };
CellValue cellValue1 = new CellValue();
cellValue1.Text = "0";
cell1.Append(cellValue1);
row1.Append(cell1);
sheetData.Append(row1);
SharedStringTablePart sharedStringTablePart1 = workbookPart.AddNewPart<SharedStringTablePart>("rId4");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
excel.Close();
}
FileStream fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "MultipleColorsInACell.xlsx", FileMode.Create, FileAccess.Write);
memoryStream.WriteTo(fileStream);
fileStream.Close();
memoryStream.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
// logging, etc.
}
}
// Generates content of sharedStringTablePart1.
private static void GenerateSharedStringTablePart1Content(SharedStringTablePart sharedStringTablePart1)
{
SharedStringTable sharedStringTable1 = new SharedStringTable() { Count = (UInt32Value)1U, UniqueCount = (UInt32Value)1U };
SharedStringItem sharedStringItem1 = new SharedStringItem();
Run run1 = new Run();
RunProperties runProperties1 = new RunProperties();
FontSize fontSize4 = new FontSize() { Val = 11D };
Color color4 = new Color() { Rgb = "0A0A09" };
RunFont runFont1 = new RunFont() { Val = "Calibri" };
FontFamily fontFamily1 = new FontFamily() { Val = 2 };
FontScheme fontScheme5 = new FontScheme() { Val = FontSchemeValues.Minor };
runProperties1.Append(fontSize4);
runProperties1.Append(color4);
runProperties1.Append(runFont1);
runProperties1.Append(fontFamily1);
runProperties1.Append(fontScheme5);
Text text1 = new Text();
text1.Text = "Requested by John S. (regular officer) - 20.10.19,";
run1.Append(runProperties1);
run1.Append(text1);
Run run2 = new Run();
RunProperties runProperties2 = new RunProperties();
FontSize fontSize5 = new FontSize() { Val = 11D };
Color color5 = new Color() { Rgb = "4A79B1" };
RunFont runFont2 = new RunFont() { Val = "Calibri" };
FontFamily fontFamily2 = new FontFamily() { Val = 2 };
FontScheme fontScheme6 = new FontScheme() { Val = FontSchemeValues.Minor };
runProperties2.Append(fontSize5);
runProperties2.Append(color5);
runProperties2.Append(runFont2);
runProperties2.Append(fontFamily2);
runProperties2.Append(fontScheme6);
Text text2 = new Text() { Space = SpaceProcessingModeValues.Preserve };
text2.Text = "Approved by Franchesco N. (supervising manager) - 22.10.19,";
run2.Append(runProperties2);
run2.Append(text2);
Run run3 = new Run();
RunProperties runProperties3 = new RunProperties();
FontSize fontSize6 = new FontSize() { Val = 11D };
Color color6 = new Color() { Rgb = "728D3C" };
RunFont runFont3 = new RunFont() { Val = "Calibri" };
FontFamily fontFamily3 = new FontFamily() { Val = 2 };
FontScheme fontScheme7 = new FontScheme() { Val = FontSchemeValues.Minor };
runProperties3.Append(fontSize6);
runProperties3.Append(color6);
runProperties3.Append(runFont3);
runProperties3.Append(fontFamily3);
runProperties3.Append(fontScheme7);
Text text3 = new Text();
text3.Text = "Signed by Koglovomor I. (CTO) - 22.10.19";
run3.Append(runProperties3);
run3.Append(text3);
sharedStringItem1.Append(run1);
sharedStringItem1.Append(run2);
sharedStringItem1.Append(run3);
sharedStringTable1.Append(sharedStringItem1);
sharedStringTablePart1.SharedStringTable = sharedStringTable1;
}
}
}
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
使用制度;
使用系统数据;
使用System.IO;
名称空间多重颜色
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
WorkbookPart WorkbookPart=null;
尝试
{
使用(var memoryStream=new memoryStream())
{
使用(var excel=SpreadsheetDocument.Create(memoryStream,DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook,true))
{
workbookPart=excel.AddWorkbookPart();
workbookPart.工作簿=新工作簿();
uint sheetId=1;
excel.WorkbookPart.Workbook.Sheets=新工作表();
Sheets Sheets=excel.WorkbookPart.Workbook.GetFirstChild();
字符串关系shipId=“rId1”;
WorksheetPart wSheetPart=workbookPart.AddNewPart(relationshipId);
string sheetName=“MultipleColorsInACell”;
Sheet Sheet=new Sheet(){Id=relationshipId,SheetId=SheetId,Name=sheetName};
附页(页);
工作表=新工作表();
wSheetPart.Worksheet=工作表;
SheetData SheetData=新的SheetData();
工作表。附加(表数据);
字符串[]列=新字符串[]{“A”、“B”、“C”、“D”、“E”、“F”、“G”};
row1=newrow(){RowIndex=(UInt32Value)5U,span=newlistvalue(){InnerText=“9:9”},dydence=0.3D};
Cell cell1=new Cell(){CellReference=“I5”,数据类型=CellValues.SharedString};
CellValue cellValue1=新的CellValue();
cellValue1.Text=“0”;
cell1.Append(cellValue1);
行1.追加(单元格1);
sheetData.Append(第1行);
SharedStringTablePart sharedStringTablePart1=workbookPart.AddNewPart(“rId4”);
生成sharedStringTablePart1内容(sharedStringTablePart1);
excel.Close();
}
FileStream FileStream=新FileStream(AppDomain.CurrentDomain.BaseDirectory+“MultipleColorsNacell.xlsx”,FileMode.Create,FileAccess.Write);
memoryStream.WriteTo(文件流);
fileStream.Close();
memoryStream.Close();
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
捕获(例外情况除外)
{
//伐木等。
}
}
//生成sharedStringTablePart1的内容。
私有静态void GenerateSharedStringTablePart1内容(SharedStringTablePart sharedStringTablePart1)
{
SharedStringTable sharedStringTable1=新的SharedStringTable(){Count=(UINT32值)1U,UniqueCount=(UINT32值)1U};
SharedStringItem sharedStringItem1=新的SharedStringItem();
运行run1=新运行();
RunProperties runProperties1=新的RunProperties();
FontSize fontSize4=新FontSize(){Val=11D};
Color color4=新颜色(){Rgb=“0A0A09”};
RunFont runFont1=新的RunFont(){Val=“Calibri”};
FontFamily fontFamily1=新FontFamily(){Val=2};
FontScheme fontScheme5=新FontScheme(){Val=FontSchemeValues.Minor};
runProperties1.Append(fontSize4);
runProperties1.Append(color4);
runProperties1.Append(runFont1);
runProperties1.Append(fontFamily1);
runProperties1.Append(fontScheme5);
Text text1=新文本();
text1.Text=“由John S(正规军官)-20.10.19请求,”;
run1.Append(runProperties1);
run1.Append(text1);
Run run2=新运行();
RunProperties runProperties2=新的RunProperties();
FontSize fontSize5=新FontSize(){Val=11D};
Color color5=新颜色(){Rgb=“4A79B1”};
RunFont runFont2=新的RunFont(){Val=“Calibri”};
FontFamily fontFamily2=新FontFamily(){Val=2};
FontScheme fontScheme6=new FontScheme(){Val=FontSchemeValues.Minor};
runProperties2.Append(fontSize5);
runProperties2.Append(color5);
runProperties2.Append(runFont2);
runProperties2.Append(fontFamily2);
runProperties2.Append(fontScheme6);
Text text2=new Text(){Space=SpaceProcessingModeValues.Preserve};
text2.Text=”