Insert background image in Excel

  • VSTO
  • Spire.XLS
  • Download Sample Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;

namespace VSTO
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Instantiate the Application object
            Excel.Application ExcelApp = Application;

            //Add a workbook
            Excel.Workbook workbook = ExcelApp.Workbooks.Add();

            //Get the first sheet
            Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];

            //Set a background image
            sheet.SetBackgroundPicture(@"C:\Users\Administrator\Desktop\image.jpg");

            //Save the file
            workbook.SaveAs("BackgroundImage.xlsx");
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing;

namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a new instance of Workbook
            Workbook workbook = new Workbook();

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Open an image 
            Bitmap bm = new Bitmap(Image.FromFile(@"C:\Users\Administrator\Desktop\image.jpg"));

            //set the image to be background image of the worksheet
            sheet.PageSetup.BackgoundImage = bm;

            //Save and Launch
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");


        }
    }
}