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
AttachmentsExport to Excel with image (31259-11142-Excelwithimage.rar)
For more details, visit http://jagadesh97cs.blogspot.com/2009/08/export-to-excel-with-image.html
|
| Author: Filip 31 Aug 2009 | Member 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 2009 | Member Level: Gold Points : 0 |
Thanks For giving advertisement to GemBox.Spreadsheet Mr. Filip.
|