Trying to insert an image file into excel spread sheet
-
Tuesday, November 16, 2010 6:57 PM
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
{
}
}- Moved by Mike Dos ZhangMicrosoft Contingent Staff Friday, November 19, 2010 8:32 AM excel develop question (From:Visual C# Language)
All Replies
-
Friday, November 19, 2010 8:32 AM
Hi mchase1,
Welcome to MSDN Forums!
I move this thread here because of it's likely to get quicker and better responses to Excel develop issues at http://social.msdn.microsoft.com/Forums/en-US/exceldev/threads where Excel develop experts live in.
Best wishes,
Mike
*****************************************************
Sample world! You will get more from this world!
Welcome to the new world!
-
Friday, November 19, 2010 12:28 PM
Looks to me like you might not have the image in the clipboard before pasting. See here:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.paste(VS.80).aspx
Hope this helps,
Eric
- Marked As Answer by Bruce SongModerator Tuesday, November 23, 2010 8:47 AM
-
Tuesday, November 23, 2010 2:38 AMModerator
Hi Mchase1,
Welcome to MSDN forum and I am glad to help with you.
According to your description, you want to insert a picture into the Excel file. If I have misunderstood you, please let me know.
As far as I know, you can store the image in the clipboard and then paste which just as Evohnave said, I have a similar thread about this issue for you to take reference:
http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/6a5b97c2-b7c4-4264-ac6a-773724c1cab4/#e398f943-9cc1-4b37-aa8a-965b5c1e4c12
I hope it can help you and feel free to follow up after you have tried the solution.
Best Regards,
Bruce Song
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Bruce SongModerator Tuesday, November 23, 2010 8:47 AM

