Vb.net 找不到类型“Integer”的公共成员“Value”时出错。将数据从datagridview(包括图像)导出到excel
我有一个datagrid,它有两个图像列。我想将数据导出到excel。使用这个代码Vb.net 找不到类型“Integer”的公共成员“Value”时出错。将数据从datagridview(包括图像)导出到excel,vb.net,excel,datagrid,Vb.net,Excel,Datagrid,我有一个datagrid,它有两个图像列。我想将数据导出到excel。使用这个代码 SaveFileDialog1.Filter = "Excel Files (*.xlsx*)|*.xlsx" SaveFileDialog1.ShowDialog() Dim filename As String = SaveFileDialog1.FileName 'verfying the datagridview having da
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
'Error = Publicmember 'Value' on type 'Integer' not found.
Dim data As Byte() = DirectCast(cj.Value, 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
找不到类型“Integer”的公共成员“Value”时出错。虽然这个条件适用于itextsharp,但我指的是PDF创建时间。请帮帮我。如果删除该条件并运行代码,它将创建一个excel文件,图像列为“System.Byte[]”。我感觉您认为您正在将网格单元存储在cj中,然后获取该单元的值,但实际上您已经获取了该单元的值并将其放入cj中。删除以下行中的Value属性访问,您的代码可能会正常工作:
Dim cj = DataGridView1.Rows(i).Cells(j).Value
顺便说一下,不要这样做:
If (cj.Value.GetType = GetType(Byte())) Then
最好单独执行此操作:
If TypeOf cj.Value Is Byte() Then
但是,如果您将强制转换为该类型,那么您应该改用TryCast:
TryCast与DirectCast类似,只是如果强制转换失败,它不会返回任何内容,而不是抛出异常。错误的意思是它所说的。在cj中有一个DGV单元格值,在本例中是一个整数,Int32没有一个值property@Plutonix问题出在cj的“.value”上。按照jmchilinney的建议移除后,条件起作用,但我没有得到所需的输出,即excel工作表,包括来自DataGridView的图像我现在更改了条件,字节数组转换为图像,但当我运行代码时,生成的excel文件显示“System.Drawing.Bitmap”而不是图像。这表示图像对象正在调用其ToString方法,并且用于填充单元格的结果。如果你不明确地说,我不知道解决办法。
Dim data As Byte() = TryCast(cj.Value, Byte())
If data IsNot Nothing Then
'Use data here.
End If