.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export Image to Microsoft Office Excel using C#

Posted By:Suthish Nair       Posted Date: August 29, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

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


Here I 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, 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;
}



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend