How to insert a background picture in excel

In the provided C# source code, we make use of the Microsoft Excel 12.0 Object Library to incorporate Excel functionalities into our application. As discussed in the preceding section, we have already covered the process of importing the Microsoft Excel 12.0 Object Library into our C# project, enabling us to seamlessly integrate Excel capabilities. By leveraging this library, we can now proceed with implementing Excel-related features within our C# application.

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(); } } } }