How to Use C# Add Excel Background Color
Add background color to highlight column labels or totals can make Excel Spreadsheet easier to read and use. This posting will show you a solution of how to add background color into Excel spreadsheet. Microsoft Excel supports colors for fonts and background fills through color palette which is an array or series of 56 RGB colors. We can the Patterns option to add Excel background color and patterns through Microsoft Excel. But here we don't use Microsoft Excel. Through C#, we can also add Excel background color without any help of Microsoft Excel.
Through the help of Spire.XLS, Excel developers can easily use C# add Excel background color. Spire.XLS is an all-in-one .NET/Silverlight Excel component which can use C#/VB.NET/Silverlight to operate Excel files without Microsoft Excel installed on system. Make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the guide below to add Excel background color.
Step 1 Create Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using Spire.Xls;
namespace FilesMerge
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load Excel File
Put the Excel file which we need add background color into the project folder. Use the following code to load it into the project.
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\..\..\Data\country.xls",ExcelVersion.Version97to2003);
Worksheet worksheet = workbook.Worksheets[0];
Step 3 Add Excel Background Color
Spire.XLS allows users to choose the Excel cell range and offers many color choices.
//set the backgroundcolor=LightBlue of Range["A2:E9"]
worksheet.Range["A2:E9"].Style.Color = Color.LightBlue;
//set the backgroundcolor=Silver of Range["A11:E18"]
worksheet.Range["A11:E18"].Style.Color = Color.Silver;
Step 4 Save and Preview
After adding Excel background color, use the code below to save and preview.
workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
System.Diagnostics.Process.Start(workbook.FileName);
Press F5 to start the project and an Excel file will be generated automatically.
Effective Screenshot:
More about Spire.XLS
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…