Export to Excel with image

Posted by Tamilmaran under VB.NET category on | Views : 2373
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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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)
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
MessageBox.Show("image inserted!")
End Sub

End Class

Comments or Responses

Login to post response