C# 如何设置单元格';背景?

C# 如何设置单元格';背景?,c#,excel,openxml,C#,Excel,Openxml,如何在OpenXml中设置一行(或整行)中多个单元格的背景 读过几篇文章: 我还是不能让它工作 我的任务实际上是乍一看似乎比较简单,而且与那些文章中所写的有点不同。上面提到的教程主要介绍如何创建新文档并设置其样式。而我需要改变现有的样式 也就是说,我有一个现有的xlsx文档(一个报告模板)。我用必要的值填充报告(由于和而得以实现)。但接下来我需要用红色背景标记几行 我既不确定是否要使用CellStyle,也不确定是否应该使用CellFormat或其他什么……这就是我到目前为止得到的: Spr

如何在OpenXml中设置一行(或整行)中多个单元格的背景

读过几篇文章:

  • 我还是不能让它工作

    我的任务实际上是乍一看似乎比较简单,而且与那些文章中所写的有点不同。上面提到的教程主要介绍如何创建新文档并设置其样式。而我需要改变现有的样式

    也就是说,我有一个现有的xlsx文档(一个报告模板)。我用必要的值填充报告(由于和而得以实现)。但接下来我需要用红色背景标记几行

    我既不确定是否要使用
    CellStyle
    ,也不确定是否应该使用
    CellFormat
    或其他什么……这就是我到目前为止得到的:

    SpreadsheetDocument doc = SpreadsheetDocument.Open("ole.xlsx", true);
    
    Sheet sheet = (Sheet)doc.WorkbookPart
                            .Workbook
                            .Sheets
                            .FirstOrDefault();
    
    WorksheetPart worksheetPart = (WorksheetPart)doc.WorkbookPart
                                                    .GetPartById(sheet.Id);
    Worksheet worksheet = worksheetPart.Worksheet;
    
    
    CellStyle cs = new CellStyle();
    cs.Name = StringValue.FromString("Normal");
    cs.FormatId = 0;
    cs.BuiltinId = 0;
    //where are the style values?
    
    WorkbookStylesPart wbsp = doc.WorkbookPart
                                    .GetPartsOfType<WorkbookStylesPart>()
                                    .FirstOrDefault();
    wbsp.Stylesheet.CellStyles.Append(cs);
    wbsp.Stylesheet.Save();
    
    
    
    Cell cell = GetCell(worksheet, "A", 20);
    cell.StyleIndex = 1U;//get the new cellstyle index somehow
    
    doc.Close();
    
    SpreadsheetDocument doc=SpreadsheetDocument.Open(“ole.xlsx”,true);
    工作表=(工作表)doc.WorkbookPart
    .工作手册
    .床单
    .FirstOrDefault();
    工作表部件工作表部件=(工作表部件)doc.WorkbookPart
    .GetPartById(第页Id);
    工作表=工作表零件工作表;
    CellStyle cs=新的CellStyle();
    cs.Name=StringValue.FromString(“正常”);
    cs.FormatId=0;
    cs.BuiltinId=0;
    //样式值在哪里?
    WorkbookStylesPart wbsp=doc.WorkbookPart
    .GetPartsOfType()
    .FirstOrDefault();
    wbsp.Stylesheet.CellStyles.Append(cs);
    wbsp.Stylesheet.Save();
    单元格=GetCell(工作表“A”,20);
    cell.StyleIndex=1U//以某种方式获取新的cellstyle索引
    doc.Close();
    

    事实上,我非常希望能有一个更轻、更简单的示例,说明如何设置单元格
    A20
    或范围从
    A20
    J20
    的样式。或者可能是指向某个更连续教程的链接。

    最后,我改变主意使用单元格背景和字体。由于foson在中的回答,我成功地添加了一个新的
    字体
    和一个新的
    单元格格式
    ,保留了原始单元格的格式(即仅更改了字体颜色):

    SpreadsheetDocument doc=SpreadsheetDocument.Open(“1.xlsx”,true);
    工作表=(工作表)doc.WorkbookPart.Workbook.Sheets.FirstOrDefault();
    工作表部件工作表部件=(工作表部件)doc.WorkbookPart
    .GetPartById(第页Id);
    工作表=工作表零件工作表;
    WorkbookStylesPart样式=doc.WorkbookPart.WorkbookStylesPart;
    样式表样式表=styles.Stylesheet;
    CellFormats CellFormats=样式表.CellFormats;
    字体=样式表。字体;
    UInt32 fontIndex=fonts.Count;
    UInt32 formatIndex=cellformats.Count;
    Cell Cell=GetCell(工作表“A”,19);
    cell.CellValue=新的CellValue(DateTime.Now.ToLongTimeString());
    cell.DataType=新的枚举值(CellValues.String);
    CellFormat f=(CellFormat)cellformats.ElementAt((int)cell.StyleIndex.Value);
    var font=(font)fonts.ElementAt((int)f.FontId.Value);
    var newfont=(Font)Font.Clone();
    newfont.Color=new Color(){Rgb=new HexBinaryValue(“ff0000”)};
    字体。追加(newfont);
    CellFormat newformat=(CellFormat)f.Clone();
    newformat.FontId=fontfindex;
    cellformats.Append(newformat);
    Save();
    cell.StyleIndex=formatIndex;
    doc.Close();
    
    您有3个选项:

  • 使用MS lib ExcelDataReader,这需要您的服务器安装Office,如果您的程序在IIS中运行,则通常不起作用

  • 使用封闭源库

  • 使用OpenXML

  • 使用纯OpenXML尝试我的代码:

    问题是关于单元格的背景,但代码只涉及字体颜色!!
    SpreadsheetDocument doc = SpreadsheetDocument.Open("1.xlsx", true);
    Sheet sheet = (Sheet)doc.WorkbookPart.Workbook.Sheets.FirstOrDefault();
    WorksheetPart worksheetPart = (WorksheetPart)doc.WorkbookPart
                                                    .GetPartById(sheet.Id);
    Worksheet worksheet = worksheetPart.Worksheet;
    
    WorkbookStylesPart styles = doc.WorkbookPart.WorkbookStylesPart;
    Stylesheet stylesheet = styles.Stylesheet;
    CellFormats cellformats = stylesheet.CellFormats;
    Fonts fonts = stylesheet.Fonts;
    
    UInt32 fontIndex = fonts.Count;
    UInt32 formatIndex = cellformats.Count;
    
    Cell cell = GetCell(worksheet, "A", 19);
    cell.CellValue = new CellValue(DateTime.Now.ToLongTimeString());
    cell.DataType = new EnumValue<CellValues>(CellValues.String);
    
    CellFormat f = (CellFormat)cellformats.ElementAt((int)cell.StyleIndex.Value);
    
    var font = (Font)fonts.ElementAt((int)f.FontId.Value);
    var newfont = (Font)font.Clone();
    newfont.Color = new Color() { Rgb = new HexBinaryValue("ff0000") };
    fonts.Append(newfont);
    
    CellFormat newformat = (CellFormat)f.Clone();
    newformat.FontId = fontIndex;
    cellformats.Append(newformat);
    
    stylesheet.Save();
    
    cell.StyleIndex = formatIndex;
    doc.Close();