Setting a background in Excel can offer several benefits that enhance the usability and visual appeal of your workbook. For example, you can set background colors or images that align with company branding or specific themes, making your spreadsheets visually cohesive with other marketing or business materials. In this article, you will learn how set a background color or image for an Excel worksheet in C# using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Add a Background Color for an Excel Worksheet
Spire.XLS for .NET provides the CellRange.Style.Color property to set the background color for a specified cell range. The following are the detailed steps:
- Create a Workbook object.
- Load an Excel workbook using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get the used range in the worksheet through Worksheet.AllocatedRange property.
- Set a background color for the used range through CellRange.Style.Color property.
- Save the result file using Workbook.SaveToFile() method.
- C#
using Spire.Xls; using System.Drawing; namespace ExcelBackgroundColor { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel workbook workbook.LoadFromFile("Budget.xlsx"); // Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the used range of the worksheet CellRange usedRange = sheet.AllocatedRange; // Set the background color of the range usedRange.Style.Color = Color.FromArgb(200, 233, 255); // Save the workbook workbook.SaveToFile("ExcelBackgroundColor.xlsx", ExcelVersion.Version2016); } } }
Add a Background Image for an Excel Worksheet
To insert a background image in Excel, you can load an image first and then set it as the worksheet background through the Worksheet.PageSetup.BackgroundImage property. The following are the detailed steps:
- Create a Workbook object.
- Load an Excel workbook using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Load an image using Image.FromFile() method.
- Set the image as the background of the worksheet through Worksheet.PageSetup.BackgroundImage property.
- Save the result file using Workbook.SaveToFile() method.
- C#
using Spire.Xls; using System.Drawing; namespace ExcelBackgroundImage { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel workbook workbook.LoadFromFile("Budget.xlsx"); // Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; // Load an image Bitmap image = new Bitmap(Image.FromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg")); // Set the background of the worksheet sheet.PageSetup.BackgoundImage = image; // Save the workbook workbook.SaveToFile("ExcelBackgroundImage.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.