使用Open XML C#在excel的第一个单元格中填充背景颜色

时间:2016-08-10 14:54:01

标签: c# openxml openxml-sdk

我正在尝试在excel的第一个单元格中填充背景颜色。下面是我用来访问excel&的单元格的代码。填充颜​​色,但它不起作用。有人可以通过告诉我我做错了什么来帮助我,还是有其他方法可以做到这一点?

下面是我为此部分使用的代码:

WorkbookPart workbookPart = myWorkbook.WorkbookPart;
                WorksheetPart worksheetPart = null;
                workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
                workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
                if (!string.IsNullOrEmpty(sheetName))
                {
                    Sheet ss = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).SingleOrDefault<Sheet>();
                    worksheetPart = (WorksheetPart)workbookPart.GetPartById(ss.Id);
                }
                else
                {
                    worksheetPart = workbookPart.WorksheetParts.FirstOrDefault();
                }
                int cellIndex = 0;
                if (worksheetPart != null)
                {
                    SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

            Row FirstRow = GetRow(worksheetPart.Worksheet, 0);


                    Cell cell1Legend = GetCell(worksheetPart.Worksheet, "A", 1);


                uint fillIndex = AddFillByColor(myWorkbook.WorkbookPart.WorkbookStylesPart, System.Drawing.Color.Yellow);

                    uint cellformatIndex = AddCellFormatWithFill(myWorkbook.WorkbookPart.WorkbookStylesPart, fillIndex);

                    CellAppendCellFormat(Cell1Legend, cellformatIndex);

0 个答案:

没有答案