2 Replies - 425 Views - Last Post: 13 September 2012 - 12:23 AM Rate Topic: -----

#1 Sunny.W  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 31-August 12

Excel Background Image C#

Posted 07 September 2012 - 12:23 AM

Hello everybody,

Do you know how to add background image for Excel file by using C#? If you know, please show me you code, OK? I am bothered with this question for a long time. Thanks very much
Is This A Good Question/Topic? 1
  • +

Replies To: Excel Background Image C#

#2 Windina  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 20-August 12

Re: Excel Background Image C#

Posted 07 September 2012 - 01:22 AM

Look at below code:
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.ApplicationClass();
            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] = "http://csharp.net-informations.com";
            xlWorkSheet.Cells[2, 1] = "Adding background in Excel File";


            xlWorkBook.SaveAs("csharp.net-informations.xls", 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();
            }
        } 

    }
}

Was This Post Helpful? 1
  • +
  • -

#3 Sunny.W  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 31-August 12

Re: Excel Background Image C#

Posted 13 September 2012 - 12:23 AM

Hi Windina,

Thanks very much. You help me a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1