datagridview到Excel Sheet包括图像 [英] datagridview to Excel Sheet including images
本文介绍了datagridview到Excel Sheet包括图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码将数据从datagrid导出到excel但它不支持图像。请帮帮我
I have the following code to export data from datagrid to excel but it doesn't support images. please help me
SaveFileDialog1.Filter = "Excel Files (*.xlsx*)|*.xlsx"
SaveFileDialog1.ShowDialog()
Dim filename As String = SaveFileDialog1.FileName
'verfying the datagridview having data or not
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'add column to that table
For i As Integer = 0 To DataGridView1.ColumnCount - 1
dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
Next
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DataGridView1.Columns.Count - 1
Dim cj = DataGridView1.Rows(i).Cells(j).Value
If (cj.GetType = GetType(Byte())) Then
Dim data As Byte() = DirectCast(cj, Byte())
Dim ms As New System.IO.MemoryStream(data)
Dim k As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
dr1(j) = k
Else
dr1(j) = DataGridView1.Rows(i).Cells(j).Value
End If
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Excel.Application()
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim strFileName As String = filename
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True
推荐答案
检查以下链接
http://vb.net-informations.com/excel-2007/vb.net_excel_200 7_insert_picture.htm [ ^ ]
http://stackoverflow.com/questions/11716873/how-to-insert-a-picture-in-to-excel-from-c -sharp-app [ ^ ]
希望以上链接有帮助。
Check the following links
http://vb.net-informations.com/excel-2007/vb.net_excel_2007_insert_picture.htm[^]
http://stackoverflow.com/questions/11716873/how-to-insert-a-picture-in-to-excel-from-c-sharp-app[^]
Hope the above links help.
这篇关于datagridview到Excel Sheet包括图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文