Home / ASP.NET Wiki / Data Access / Export Image to Excel using c#

Export Image to Excel using c#

 Rate It (4)

 

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;
}

Revision number 4, Sunday, August 23, 2009 2:40:33 PM by
This is not the most up to date version of this article. The most recent version can be found here.

Comments

Good work!!

nice code!

When i try to run This part objSheet.Shapes.AddPicture(sFileImage, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 700, 350) has following error Y Please send me Reference required to assembly 'Office, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' containing the type 'Microsoft.Office.Core.MsoTriState'. Add one to your project

Helpful

good work :)

we can use open xml insted of dll.

Is there way to import image from excel.

Thanks for the article...Really nice...

good one.

Nice technique. Very helpful.

Great work... Easy to read and easy to understand!

it work nice but not for web application, it ask for identity . so it is not work in web application without server username and password.. if ant other solution is there for Export image to excel

Can I also export data to excel with the image

ashoksudani.. The sample provided from web application only.. Might you need some security settings for excel..

here the objWorkBook.SaveAs saves the file on the server.. how can I get it to save the file on the client side?

When i debug the WCF Project in Visual studio environment it works fine. But when i published in IIS web server 7.5 and Create a Virtual directory and run it then not working.

IIS not working with interop

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. proffy (1)
  2. primillo (1)