How to insert a background picture in excel

We use the Microsoft Excel 12.0 Object Library here in our implementation to interface with the Excel functions directly. This information was discussed in the previous section that has already confirms the process of importing the Microsoft Excel 12.0 Object Library into our C# project which offers the possibility to combine Excel functionalities with ease. So with the help of this library, we have reached the stage where we can now move ahead with the process of implementing excel-related features in our C# app.

Create Excel file from CSharp

To insert a background picture in Excel using C#, you can utilize the SetBackgroundPicture method of the worksheet object. This method allows you to set a picture as the background of the worksheet. By calling the SetBackgroundPicture method and passing the appropriate parameters, such as the path to the picture file, you can insert a background picture in Excel from your C# application.

SetBackgroundPicture(string filename)
  1. Filename : The background picture filename.

After you execute the C# source code you will get the Excel file like the following picture.

csharp-excel-background-picture Full Source C#
using System; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.SetBackgroundPicture("C:\\csharp-xl-picture.JPG"); //add some text xlWorkSheet.Cells[1, 1] = ""; xlWorkSheet.Cells[2, 1] = "Adding background in Excel File"; xlWorkBook.SaveAs("", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlApp); releaseObject(xlWorkBook); releaseObject(xlWorkSheet); MessageBox.Show ("File created !"); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } } } }