.NET Excel Layout-Add Excel Background Image in C# and Visual Basic

Each company must have a lot of reports to present monthly sales, financial situation etc. And these reports are often released with a great appearance and layout, for example, well-designed cells or with beautiful charts. If we want to create a wonderful report, how to do?

As is known, Excel is very powerful on displaying, editing and calculating data information so that it was recognized as one of the best choice to create report. Now, the data information has been imported to Excel and next we need to format this Excel to be a appealed one. In this post, I will introduce a solution to make an Excel attractive through adding background image in C# and Visual Basic.

In this example, a .NET Excel component  is used so that I have added its dll as reference in my project.

Firstly, load this Excel file and initialize a Worksheet instance. Secondly, get a Bitmap image instance from specified path. Thirdly, assign this image instance as value for BackgroundImage property of PageSetup of this sheet instance. Finally, save the new Excel file and launch for viewing.

Please follow the code below to insert Excel background image.

CODE:

C#

using System.Drawing;

using Spire.Xls;

 

namespace ExcelBackgroundImage

{

    class Background

    {

        static void Main(string[] args)

        {

            //Load File

            Workbook workbook = new Workbook();

            workbook.LoadFromFile(@”E:\Work\Documents\ExcelFiles\VendorInfo.xlsx”);

            Worksheet sheet = workbook.Worksheets[0];

 

            //Set Background Image

            Bitmap bm = new Bitmap(Image.FromFile(@”E:\Work\Documents\SampleImage\X.jpg”));

            sheet.PageSetup.BackgoundImage = bm;

 

            //Save and Launch

            workbook.SaveToFile(“Background.xlsx”, ExcelVersion.Version2010);

            System.Diagnostics.Process.Start(“Background.xlsx”);

        }

    }

}

 

VB

Imports System.Drawing

Imports Spire.Xls

 

Namespace ExcelBackgroundImage

    Friend Class Background

        Shared Sub Main(ByVal args() As String)

            ‘Load File

            Dim workbook As New Workbook()

            workbook.LoadFromFile(“E:\Work\Documents\ExcelFiles\VendorInfo.xlsx”)

            Dim sheet As Worksheet = workbook.Worksheets(0)

 

            ‘Set Background Image

            Dim bm As New Bitmap(Image.FromFile(“E:\Work\Documents\SampleImage\X.jpg”))

            sheet.PageSetup.BackgoundImage = bm

 

            ‘Save and Launch

            workbook.SaveToFile(“Background.xlsx”, ExcelVersion.Version2010)

            System.Diagnostics.Process.Start(“Background.xlsx”)

        End Sub

    End Class

End Namespace

         

RESULT

DOWNLOAD Spire.XLS for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s