Mangesh barmate

Mangesh barmate

  • NA
  • 118
  • 136.8k

Error while coding for Excel sheet formatting in c#?

Apr 18 2013 11:03 AM
 

Hi,
I am having excel generating code but when i am using StyleSheet class it gives me one error.
Below are my code and error Message-->
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
private static void GenerateSpreadSheet(string fileName)
        {
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
            {
                WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
                WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                Workbook workbook = new Workbook();

                string worksheetPartId = workbookPart.GetIdOfPart(worksheetPart);

                FileVersion fileVersion = new FileVersion { ApplicationName = "Microsoft Office Excel" };
                Worksheet worksheet = new Worksheet();
                SheetData sheetData = new SheetData();
                string xmlData = string.Empty;
                string platformCode = string.Empty;
                //string salesPropertyFieldColumnName = string.Empty;
                UInt32 rowIndex = 1;
                int columnIndex = 1;
                Form1 form1 = new Form1();               

                //STYLESHEET CODE
                Stylesheet styleSheet = workbook.WorkbookStylesPart.Stylesheet;

HERE I DON'T UNDERSTAND WHATTO USE FOR OVERCOME STYLESHEET ERROR.

                //build the formatted header style
                UInt32Value headerFontIndex =
                   form1.createFont(
                       styleSheet,
                        "Arial",
                       12,
                        true,
                       System.Drawing.Color.Red);
                //set the background color style
                UInt32Value headerFillIndex =
                   form1.createFill(
                       styleSheet,
                       System.Drawing.Color.DarkBlue);
                //create the cell style by combining font/background
                UInt32Value headerStyleIndex =
                   form1.createCellFormat(
                       styleSheet,
                       headerFontIndex,
                       headerFillIndex,
                        null);

                //Populating Header of First Row.
                Row dataRow = CreateRow(worksheet, rowIndex, columnIndex, "Group Number", "12345", true);
                sheetData.AppendChild(dataRow);
               
                worksheet.Append(sheetData);
                worksheetPart.Worksheet = worksheet;
                worksheetPart.Worksheet.Save();
                Sheets sheets = new Sheets();
                Sheet sheet = new Sheet { Name = "Sheet1", SheetId = 1, Id = worksheetPartId };
                sheets.Append(sheet);
                workbook.Append(fileVersion);
                workbook.Append(sheets);
                spreadsheetDocument.WorkbookPart.Workbook = workbook;
                spreadsheetDocument.WorkbookPart.Workbook.Save();

                spreadsheetDocument.Close();
            
            }
        }
ERROR IS-->1              'DocumentFormat.OpenXml.Spreadsheet.Workbook' does not contain a definition for 'WorkbookStylesPart' and no extension method 'WorkbookStylesPart' accepting a first argument of type 'DocumentFormat.OpenXml.Spreadsheet.Workbook' could be found (are you missing a using directive or an assembly reference?)       
Is anyone have idea to overcome this issue.