简体   繁体   中英

Add style to Excel in openxml

I want to set forecolor of the text in the excel document which I open to write the text.

For that I tried :

var stylesheet1 = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;

                Fills fills1 = new Fills() { Count = (UInt32Value)5U };

                Fill fill5 = new Fill();
                PatternFill patternFill5 = new PatternFill() { PatternType = PatternValues.Solid };
                ForegroundColor foregroundColor3 = new ForegroundColor() { Rgb = "#FF0000" };
                patternFill5.Append(foregroundColor3);

                fill5.Append(patternFill5);

                fills1.Append(fill5);

                stylesheet1.Fills.Append(fills1);
                var fid = stylesheet1.Fills.Count++;         

                wbsp.Stylesheet = stylesheet1;

                Row excelRow;
                excelRow = new Row();
                excelRow.RowIndex = 0;

                Cell cell = new Cell()
                {
                    //create the cell reference of format A1, B2 etc
                    //CellReference = Convert.ToString(Convert.ToChar(65)),
                    CellReference = "A1",
                    DataType = CellValues.String
                };


                CellValue cellValue = new CellValue();

                cellValue.Text = "*";
                //add the value to the cell
                cell.Append(cellValue);

                CellFormat cellFormat = null;
                if (cell.StyleIndex.HasValue)
                {
                    var originalCellFormat = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ToList()[(int)cell.StyleIndex.Value] as CellFormat;
                    //copy the original cellformat data to the new cellformat
                    if (originalCellFormat != null)
                    {
                        cellFormat = new CellFormat(originalCellFormat.OuterXml);
                    }
                    else
                    {
                        cellFormat = new CellFormat();
                    }
                }
                else
                {
                    cellFormat = new CellFormat();
                }
                cellFormat.FillId = (UInt32)fid;
                stylesheet1.CellFormats.Append(cellFormat);
                var theStyleIndex = stylesheet1.CellFormats.Count++;
                cell.StyleIndex = new UInt32Value { Value = (UInt32)theStyleIndex };
                spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();

But it gives me error on the first line :

var stylesheet1 = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;

Error:

Object not set to instance of an object.

When i add a watch on code :

spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;

I find that : spreadSheet.WorkbookPart.WorkbookStylesPart is null

Please help me how can i add forecolor to cell

Perhaps the initialization of SpreadsheetDocument is missing?

using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath + ".xlsx", SpreadsheetDocumentType.Workbook))
{
    WorkbookPart workbookPart = document.AddWorkbookPart();
    workbookPart.Workbook = new Workbook();

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet();

    WorkbookStylesPart workStylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
    workStylePart.Stylesheet = new Stylesheet();
    var stylesheet1 = document.WorkbookPart.WorkbookStylesPart.Stylesheet;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM