0
votes

I have a code left by the previous developer. What I need to do is to change the background color and text color of the header columns in the Excel file. But I just couldn't. We use OpenXML. Heading columns are added as rows. I can't change background and text color of these header row. How can I change the background color of the cell interiors in the cell variable?

public byte[] analizVerileriExport(DataTable data)
    {
        byte[] result;
        using (MemoryStream mem = new MemoryStream())
        {
            using (var spreadsheetDocument = SpreadsheetDocument.Create(mem, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
            {
                // Add a WorkbookPart to the document.
                WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

                // Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                SheetData sheetData = new SheetData();
                worksheetPart.Worksheet = new Worksheet(sheetData);

                // Add Sheets to the Workbook.
                Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                    AppendChild<Sheets>(new Sheets());

                // Append a new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet()
                {
                    Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                    SheetId = 1,
                    Name = string.IsNullOrEmpty(data.TableName) ? "Analiz Verileri" : data.TableName
                };


                Row headerRow = new Row();

                List<excelColumns> columns = new List<excelColumns>();

                foreach (DataColumn column in data.Columns)
                {
                    
                    columns.Add(new excelColumns() { columnName = column.ColumnName, columnDataType = column.DataType });

                    Cell cell = new Cell();
                    cell.DataType = CellValues.String;
                    cell.CellValue = new CellValue(column.ColumnName);

                    headerRow.AppendChild(cell);
                }

                sheetData.AppendChild(headerRow);

                foreach (DataRow dsrow in data.Rows)
                {
                    Row newRow = new Row();
                    foreach (excelColumns col in columns)
                    {
                        Cell cell = new Cell();
                        cell.DataType = col.columnDataType.ToCellValue();
                        cell.CellValue = new CellValue(dsrow[col.columnName].ToString()); //
                        newRow.AppendChild(cell);
                    }

                    sheetData.AppendChild(newRow);
                }

                sheets.Append(sheet);

                workbookpart.Workbook.Save();

                // Close the document.
                spreadsheetDocument.Close();
            }
            result = mem.ToArray();
        }
        //string excelName = cmbClient.Text + " Kişisel Veri Envanteri " + String.Format("{0:dd_MM_yyyy HH-mm-ss}", DateTime.Now) + ".xlsx";
        //openFile(excelName, result);
        return result;
    }