Set cell style in Excel

  • VSTO
  • Spire.XLS
  • Download Sample Code

using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("MySheet");

            //Create cell and set its value
            ICell cell = sheet.CreateRow(1).CreateCell(3);
            cell.SetCellValue("Spire");

            //Create style
            ICellStyle style = workbook.CreateCellStyle();

            //Set border style 
            style.BorderBottom = BorderStyle.Double;
            style.BottomBorderColor = HSSFColor.Yellow.Index;

            //Set font style
            IFont font = workbook.CreateFont();
            font.Color = HSSFColor.Red.Index;
            font.FontName = "Arial";
            font.FontHeight = 13;
            font.IsItalic = true;
            style.SetFont(font);

            //Set background color
            style.FillBackgroundColor = IndexedColors.Black.Index;
            style.FillPattern = FillPattern.SolidForeground;

            //Apply the style
            cell.CellStyle = style;

            //Save the file
            FileStream file = File.Create("CellStyle.xlsx");
            workbook.Write(file);
            file.Close();

            //Launch the file
            System.Diagnostics.Process.Start("CellStyle.xlsx");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create workbook
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.CreateEmptySheet("MySheet");

            //Get the cell and set its text
            CellRange range = sheet.Range["C3"];
            range.Text="Spire";

            //Create style 
            CellStyle style = workbook.Styles.Add("MyStyle");

            //Set background color
            style.Color = Color.Black;
            style.FillPattern = ExcelPatternType.Solid;

            //Set border style
            style.Borders.Color = Color.Yellow;
            style.Borders.LineStyle = LineStyleType.Thin;
            style.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
            style.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;

            //Set font style
            style.Font.FontName = "Arial";
            style.Font.IsItalic=true;
            style.Font.Color = Color.Red;
            style.Font.Size = 13;
            
            //Apply the style
            range.CellStyleName = style.Name;

            //Save and Launch
            workbook.SaveToFile("CellStyle.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("CellStyle.xlsx");
        }
    }
}