C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Export to Excel with image


Posted Date: 11 Aug 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Tamil MaranMember Level: Gold    
Rating: 1 out of 5Points: 10



Export to Excel with image



This VB.NET application creates a excel file with image. Proper commands are added along with the code for easy understanding

(Note Before going to run this code give the available image name to the img variable)



Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddImageIntoExcel()
End Sub
Dim Img As Bitmap = Image.FromFile("c:\test1.gif")
'place u r image file name with path in above double quoted area
Private Sub AddImageIntoExcel()
'Create an Excel App
Dim excelApp As New Microsoft.Office.Interop.Excel.Application()
excelApp.Visible = False
'The Excel doc paths
Dim oMissing As Object = System.Reflection.Missing.Value
Dim destFile As String = "C:\test_image.xlsx"
Dim excelFile As String = "C:\Book1.xlsx"
'Open the worksheet file
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
excelBook = excelApp.Workbooks.Open(excelFile)
Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
excelSheet = CType(excelBook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim imgCell As Object = "G1"
Dim range As Microsoft.Office.Interop.Excel.Range
range = excelSheet.Range(imgCell)
range.Select()
Dim newImg As Bitmap = New Bitmap(80, 40)
Me.BackgroundImage = newImg
Using g As Graphics = Graphics.FromImage(newImg)
g.DrawImage(Img, New Rectangle(0, 0, newImg.Width, newImg.Height), _
0, 0, Img.Width, Img.Height, GraphicsUnit.Pixel)
End Using
Clipboard.SetDataObject(newImg)
excelSheet.Paste()
excelSheet.SaveAs(destFile)
'Quit
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
MessageBox.Show("image inserted!")
End Sub

End Class



Attachments

  • Export to Excel with image (31259-11142-Excelwithimage.rar)

    For more details, visit http://jagadesh97cs.blogspot.com/2009/08/export-to-excel-with-image.html



  • Responses

    Author: Filip    31 Aug 2009Member Level: Silver   Points : 2
    Hi nice article but,

    Best way to deal with Excel in VB is by using 3rd party component. It's much faster and easier way then Excel Interop. Try using GemBox spreadsheet component for exporting Excel. Component is free for commercial use but with limit of 150 rows.

    Here you can see a list of reason why GemBox.Spreadsheet is much better tool to work with Excel then Excel Interop.


    Author: Tamil Maran    19 Nov 2009Member Level: Gold   Points : 0
    Thanks For giving advertisement to GemBox.Spreadsheet Mr. Filip.


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Image exporting  .  Exporting image  .  Export to Excel  .  Excel exporting with image  .  Excel Exporting in VB.NET  .  Excel Export  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Null coalescing Operator
    Previous Resource: DataBinding in Combobox
    Return to Discussion Resource Index
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use