Insert Excel Background Image in C#, VB.NET

This section aims at providing developers a solution on how to insert Excel background image in C#, VB.NET via a .NET Excel library Spire.XLS for .NET.

Spire.XLS for .NET enables you to add background image in your Excel file by setting BackgroundImage property of the object PageSetup to be the image you want to insert, you can see the code here: Spire.Xls.Worksheet.PageSetup.BackgroundImage. Now let us start our task.


If you are not familiar with Spire.XLS for .NET, please allow me to give a short description of it. Spire.XLS for .NET is an Excel application which enables users to perform a wide range of processing tasks in Excel documents such as generate, edit and handle Excel files in C#.

You will use Spire.XLS for .NET, so we can download Spire.XLS for .NET first. After installing it on system, please start your project either in C# Console Application or VB.NET Console Application. You also can create your project in Windows Forms Application. Please do not forget to add Spire.Xls as reference. The default path is “..\Spire.XLS\Bin\Bin\NET4.0\ Spire.XLS.dll”. Below shows the whole code of my project:

//initialize a new instance of Workbook
Workbook workbook = new Workbook();
//import an Excel file from system
workbook.LoadFromFile(@"..\Excel background image.xlsx");
//get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//open an image 
Bitmap bm = new Bitmap(Image.FromFile(@"..\butterflytable.jpg"));
//set the image to be background image of the worksheet
sheet.PageSetup.BackgoundImage = bm;
//save excel file
//launch the file
'initialize a new instance of Workbook
Dim workbook As New Workbook()
'import an Excel file from system
workbook.LoadFromFile("..\Excel background image.xlsx")
'get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'open an image 
Dim bm As New Bitmap(Image.FromFile("..\butterflytable.jpg"))
'set the image to be background image of the worksheet
sheet.PageSetup.BackgoundImage = bm
'save excel file
'launch the file

Output File:

After executing above code, I successfully add background image in Excel file, you can see the output file as below:

Insert Excel Background Image

In this section, I have introduced the detail solution on how to insert background image in Excel file with C#, VB.NET. I wish it can help you. If you have any questions, comments or advice, you can add it at E-iceblue Forum, our professionals will give prompt reply.