vb.net 使用vb.net在excel中的特定单元格中放置图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25966991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing picture at specific cell in excel using vb.net
提问by Sahil Manchanda
I have coded the following code to export data(including images) from datagridview to excel in vb.net Everything works fine except that pictures are not placing at their desired positions. I didn't find any method that belongs to Shapes.addPicture() method to specify the position by indexes. After the creation of file all the pictures are are by default at the starting of excel like a stack. Here is the code.
我编写了以下代码以将数据(包括图像)从 datagridview 导出到 vb.net 中的 excel 一切正常,只是图片没有放置在所需的位置。我没有找到任何属于 Shapes.addPicture() 方法的方法来通过索引指定位置。创建文件后,默认情况下,所有图片都像堆栈一样位于excel的开头。这是代码。
Dim xlApp As Microsoft.Office.Interop.Excel.Application
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
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
Next
Dim count As Integer = 0
For i = 0 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1
Dim cj = DataGridView1(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 = "c:\h" + count.ToString + ".jpg"
im.Save(h, Imaging.ImageFormat.Jpeg)
xlWorkSheet.Shapes.AddPicture(h, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, i + 2, j + 1, 100, 100)
count += 1
Else
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(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 If
回答by Dale M
The leftand topparameters of the AddPicturemethod specify the left and top position of the picture in pointsfrom the top-left corner of the sheet. You seem to be thinking that this will be in cells. You need to calculate the size of your cells in points and use those figures. Pictures in excel sit "over" the grid - not "in" it.
该方法的参数left和top参数以距离工作表左上角的点数AddPicture指定图片的左侧和顶部位置。你似乎认为这将在cell 中。您需要以点为单位计算单元格的大小并使用这些数字。excel 中的图片位于网格“上方” - 而不是“位于”网格中。

