简体   繁体   English

使用vb.net将图片放置在excel中的特定单元格上

[英]Placing picture at specific cell in excel using vb.net

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. 我已经对以下代码进行了编码,以便将数据(包括图像)从datagridview导出到vb.net中的excel,除了图片未放在所需的位置之外,其他所有方法都工作正常。 I didn't find any method that belongs to Shapes.addPicture() method to specify the position by indexes. 我没有找到任何属于Shapes.addPicture()方法的方法来通过索引指定位置。 After the creation of file all the pictures are are by default at the starting of excel like a stack. 创建文件后,所有图片默认情况下都是像stack一样在excel的开头。 Here is the code. 这是代码。

 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

The left and top parameters of the AddPicture method specify the left and top position of the picture in points from the top-left corner of the sheet. lefttop的的参数AddPicture方法从片材的左上角指定的图片的左侧和顶部位置。 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. Excel中的图片“位于”网格上方-而不是“位于”网格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM