Export Image to Microsoft Office Excel using c# and Interop

Here am trying to show how we can export an image to excel file.

References Used: Microsoft.Office.Interop.Excel;

Link for downloading the dlls and install : Office XP PIAs

Add Microsoft.Office.Interop.Excel.dll to the bin folder.

Add office.dll to the bin folder.

 String sFileImage = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["UploadPath"], Session["UserId"].ToString() + ".gif");
 String sFilePath = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["UploadPath"], Session["UserId"].ToString() + ".xls");
 if (File.Exists(sFilePath)) { File.Delete(sFilePath); }

 Microsoft.Office.Interop.Excel.ApplicationClass objApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
 Microsoft.Office.Interop.Excel.Worksheet objSheet = new Microsoft.Office.Interop.Excel.Worksheet();
 Microsoft.Office.Interop.Excel.Workbook objWorkBook = null;
 //object missing = System.Reflection.Missing.Value;

 try
 {
   objWorkBook = objApp.Workbooks.Add(Type.Missing);
   objSheet = (Microsoft.Office.Interop.Excel.Worksheet)objWorkBook.ActiveSheet;

   //Add picture to single sheet1 
   objSheet = (Worksheet)objWorkBook.Sheets[1];
   objSheet.Name = "Graph with Report";

   //////////////  

   Or multiple sheets

   for (int iSheet = 0; iSheet < objWorkBook.Sheets.Count - 1; iSheet++)
   {
      objSheet = objWorkBook.Sheets[iSheet] as Worksheet;
      ///(objSheet as Microsoft.Office.Interop.Excel._Worksheet).Activate();
   }

   /////////////////

   objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350);
   objWorkBook.SaveAs(sFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
   Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 }
 catch (Exception)
 {
  //Error Alert
 }
 finally
 {
    objApp.Quit();
    objWorkBook = null;
    objApp = null;
 }
Published Monday, January 11, 2010 4:54 PM by Suthish Nair

Comments

# re: Export Image to Microsoft Office Excel using c# and Interop

Thursday, February 25, 2010 1:39 PM by Madhav

Hi Nair,

thq for the info, i tried the same as per ur instructions, when addedd interop.dll to bin folder office.dll also added. I refered the path which all these interops are available under visual studio installation, not to the path where i have unzipped.

commented  for() loop for multiple sheets case, but error with line objSheet.Shapes.AddPicture()......

is there anything to do extra or am doing something wrong?

ur help iz greatly appreciated.

thanks & regards,

Madhav

# re: Export Image to Microsoft Office Excel using c# and Interop

Friday, February 26, 2010 5:37 AM by Madhav

Hi Nair,

I have followed the same steps... but found PIA already installed so used the same.

while executing the code getting error at line

"objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350);"

I couldn't figure out why error and throwing exception.

am doing anything wrong? through me a light....

thanks

madhav

# re: Export Image to Microsoft Office Excel using c# and Interop

Friday, February 26, 2010 5:48 AM by Madhav

Hi Nair,

I have followed the same steps... but found PIA already installed so used the same.

while executing the code getting error at line

"objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350);"

I couldn't figure out why error and throwing exception.

am doing anything wrong? through me a light....

thanks

madhav

# re: Export Image to Microsoft Office Excel using c# and Interop

Friday, March 05, 2010 6:21 AM by Madhav

Hi Nair,

Good evening,

thq for the nice article and code.

I have generated line graphs and tried ur code to export to excel. but facing issue with below line

objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350);

what could be the potential problem?i couldn't trace out.

ut help z needed here.

thanks

madhav

# re: Export Image to Microsoft Office Excel using c# and Interop

Monday, March 08, 2010 12:39 PM by madhavreddych

hi nair,

thq for greeat info.. i did same but stuck at line 'objSheet.Shapes.AddPicture()' can you point what could be the error.

tried many times to write u but never succedded here, let e see my luck this time.

thanks

madhav

# re: Export Image to Microsoft Office Excel using c# and Interop

Thursday, March 25, 2010 2:25 AM by Suthish

i suggest to remove all your excel refernces from project and add again fresh excel interop references from above setup linkl posted..

# re: Export Image to Microsoft Office Excel using c# and Interop

Wednesday, May 05, 2010 8:03 AM by New Techi

Thnx man. I was breaking my head for this one. Thnx a lot

Leave a Comment

(required) 
(required) 
(optional)
(required)