none
Trying to insert an image file into excel spread sheet

    Question

  • I am new to programing C# and Excel programing. I am trying to insert an image file (Bitmap here but same problem with .gif and .jpg) into an excel spread sheet using C# from a asp.net webpage but can not seen to paste the image into the worksheet. I have an existing excel spreadsheet "template4.xls" and an existing bitmap file "SpecAnImage.bmp" both in the same directory. When I click a button on the web page I want the image to get inserted into the spreadsheet. The code seems to die on the paste statement.

    Thank you for any help you can provide.

    Here is my exception error message:

    System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at ExcelTest.btnPlaceImg_Click(Object sender, EventArgs e) in c:\Documents and Settings\mchase\my documents\visual studio 2008\websites\opticaltest\ExcelTest.aspx.cs:line 223

    Here is my stripped down code.

     

    protected void btnPlaceImg_Click(object sender, EventArgs e)
        {

                Microsoft.Office.Interop.Excel.ApplicationClass ThisApplication;

                ThisApplication = new Microsoft.Office.Interop.Excel.ApplicationClass();

                Workbook ThisWorkBook;


                string _stFileName = @"C:/Documents and Settings/mchase/My Documents/Visual Studio 2008/WebSites/OpticalTest/ContentPages/data/template4.xls";

                string _stPicture = @"C:/Documents and Settings/mchase/My Documents/Visual Studio 2008/WebSites/OpticalTest/ContentPages/data/SpecAnImage.bmp";

                object missing = System.Reflection.Missing.Value;
               
                try
                {
                  
                    ThisWorkBook = ThisApplication.Workbooks.Open(_stFileName,missing,missing,5,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);

    Microsoft.Office.Interop.Excel.Worksheet ThisSheet = (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkBook.Sheets[1];
           
                    //ThisSheet.Cells[1,1] = "test";  //just use this to verify that I connect to the spread sheet

                    Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)ThisSheet.Cells[1,1];

                    System.Drawing.Image oImage = System.Drawing.Image.FromFile(_stPicture);

                   
                    oRange.set_Item(2,2,oImage);
                    ThisSheet.Paste(oRange, _stPicture);
                    ThisWorkBook.Save();
                   
                    ThisApplication.Quit();
                    ThisWorkBook = null;
                    ThisApplication = null;
                    txtImageFile.Text = "done";
                }
                catch(Exception oEx)
                {
                    lbldied.Text = oEx.ToString()   ;
                    ThisApplication.Quit();
                    ThisWorkBook = null;
                    ThisApplication = null;
                }
                finally
                {
                   
                }

           
        }

    Tuesday, November 16, 2010 6:57 PM

Answers

All replies