exporting datagridview with images to excel using vb.net

  • Thread starter Thread starter pawiro timijo
  • Start date Start date
P

pawiro timijo

Guest
'i tried to export dtgridview contains images to excel. it works but the images are at the same cell
'could you guys fix my code

Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

Dim xlApp As Microsoft.Office.Interop.Excel.Application
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For k As Integer = 1 To datagridview.Columns.Count
xlWorkSheet.Cells(1, k) = datagridview.Columns(k - 1).HeaderText
Next

Dim count As Integer = 0
For i = 0 To datagridview.RowCount - 1
For j = 0 To datagridview.ColumnCount - 1

Dim cj = datagridview(j, i).Value
If (cj.GetType = GetType(System.Byte())) Then
Dim data As Byte() = DirectCast(cj, Byte())
Dim ms As New System.IO.MemoryStream(data)
Dim im As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
Dim h As String = "d:\h" + count.ToString + ".jpg"
im.Save(h, Imaging.ImageFormat.Png)
xlWorkSheet.Shapes.AddPicture(h, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, i + 1, j + 1, 100, 100)
count += 1

Else
xlWorkSheet.Cells(i + 2, j + 1) = datagridview(j, i).Value.ToString()
End If


Next
Next

xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
'releaseObject(xlApp)
'releaseObject(xlWorkBook)
'releaseObject(xlWorkSheet)

Dim res As MsgBoxResult
res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
If (res = MsgBoxResult.Yes) Then Process.Start("d:\vbexcel.xlsx")
End Sub



xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

Dim xlApp As Microsoft.Office.Interop.Excel.Application
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For k As Integer = 1 To datagridview.Columns.Count
xlWorkSheet.Cells(1, k) = datagridview.Columns(k - 1).HeaderText
Next

Dim count As Integer = 0
For i = 0 To datagridview.RowCount - 1
For j = 0 To datagridview.ColumnCount - 1

Dim cj = datagridview(j, i).Value
If (cj.GetType = GetType(System.Byte())) Then
Dim data As Byte() = DirectCast(cj, Byte())
Dim ms As New System.IO.MemoryStream(data)
Dim im As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
Dim h As String = "d:\h" + count.ToString + ".jpg"
im.Save(h, Imaging.ImageFormat.Png)
xlWorkSheet.Shapes.AddPicture(h, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, i + 1, j + 1, 100, 100)
count += 1

Else
xlWorkSheet.Cells(i + 2, j + 1) = datagridview(j, i).Value.ToString()
End If


Next
Next

xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
'releaseObject(xlApp)
'releaseObject(xlWorkBook)
'releaseObject(xlWorkSheet)

Dim res As MsgBoxResult
res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
If (res = MsgBoxResult.Yes) Then Process.Start("d:\vbexcel.xlsx")
End Sub
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer Dim xlApp As Microsoft.Office.Interop.Excel.Application xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") For k As Integer = 1 To datagridview.Columns.Count xlWorkSheet.Cells(1, k) = datagridview.Columns(k - 1).HeaderText Next Dim count As Integer = 0 For i = 0 To datagridview.RowCount - 1 For j = 0 To datagridview.ColumnCount - 1 Dim cj = datagridview(j, i).Value If (cj.GetType = GetType(System.Byte())) Then Dim data As Byte() = DirectCast(cj, Byte()) Dim ms As New System.IO.MemoryStream(data) Dim im As System.Drawing.Image = System.Drawing.Image.FromStream(ms) Dim h As String = "d:\h" + count.ToString + ".jpg" im.Save(h, Imaging.ImageFormat.Png) xlWorkSheet.Shapes.AddPicture(h, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, i + 1, j + 1, 100, 100) count += 1 Else xlWorkSheet.Cells(i + 2, j + 1) = datagridview(j, i).Value.ToString() End If Next Next xlWorkSheet.SaveAs("D:\vbexcel.xlsx") xlWorkBook.Close() xlApp.Quit() 'releaseObject(xlApp) 'releaseObject(xlWorkBook) 'releaseObject(xlWorkSheet) Dim res As MsgBoxResult res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo) If (res = MsgBoxResult.Yes) Then Process.Start("d:\vbexcel.xlsx") End Sub

Continue reading...
 
Back
Top