Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
165
How to apply Excel Cell Style
posted

Hello,

Can any one let me know how to apply excel cell styles? http://msdn.microsoft.com/en-us/library/f1hh9fza(v=VS.100).aspx

 

 

Parents
No Data
Reply
  • 44743
    Suggested Answer
    posted

    Are you interested in adding the workbook styles specifically or are you just looking for a way to set the style of cells? If you want to add workbook styles, you can do something like this:

    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets.Add("Sheet1");
     
    IWorksheetCellFormat style = workbook.CreateNewWorksheetCellFormat();
    style.Font.Name = "Verdana";
    style.Font.Height = 240;
    style.Font.Color = System.Drawing.Color.Red;
    style.FillPatternForegroundColor = System.Drawing.Color.Gray;
    style.FillPattern = FillPatternStyle.Solid;
    workbook.Styles.AddUserDefinedStyle(style, "NewStyle");
     
    WorksheetCell cell = worksheet.GetCell("A1");
    cell.Value = "Test";
    cell.CellFormat.SetFormatting(style);
     
    workbook.Save("Styles.xls");

     But if you just want to change the style of a cell, you can do this:

    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets.Add("Sheet1");
    			
    WorksheetCell cell = worksheet.GetCell("A1");
    cell.Value = "Test";
    cell.CellFormat.Font.Name = "Verdana";
    cell.CellFormat.Font.Height = 240;
    cell.CellFormat.Font.Color = System.Drawing.Color.Red;
    cell.CellFormat.FillPatternForegroundColor = System.Drawing.Color.Gray;
    cell.CellFormat.FillPattern = FillPatternStyle.Solid;
     
    workbook.Save("Styles.xls");
Children