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.

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:

[C#]
using Spire.Xls;
using System.Drawing;
namespace InsertExcelBackgroundImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //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
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            //launch the file
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing
Namespace InsertExcelBackgroundImage
	Class Program
		Private Shared Sub Main(args As String())
			'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
			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
			'launch the file
			System.Diagnostics.Process.Start("result.xlsx")
		End Sub
	End Class
End Namespace

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.