Click here to Skip to main content
14,216,534 members
Click here to Skip to main content
Article
Posted 11 Oct 2013

Stats

20.4K views
2 bookmarked

Export Image to Excel using c#

,
Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
11 Oct 2013     CPOL    
 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

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

 

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

760 members


Comments and Discussions

 
-- There are no messages in this forum --