How to paste image in excel cell from data grid view RRS feed

  • Question

  • HI

    I am using data grid view in I have following columns in data grid view

    Student Id


    Student PIcture

    when I copy the grid data and paste it into excel worksheet it paste all data except image data.

    please guide me how to overcome this problem either manually copy and paste or programmatically.



    Monday, September 11, 2017 1:11 PM

All replies

  • Hello,

    First off, if working with .xlsx then I highly recommend using SpreadSheetLight (free library) which can be included in your project via NuGet, from this page. Note there is a great help file on the site too.

    Basic requirement is to know the image source so it can be read from disk or you will need to get the image as a byte array from the DataGridView and use one of the SLPicture overloads for bytes

    Imports SpreadsheetLight
    Imports SpreadsheetLight.Drawing
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using sl As New SLDocument("SomeFile.xlsx", "SomeSheet")
                Dim pic As New SLPicture("C:\SomePicture.bmp")
                pic.SetPosition(2, 1)
            End Using
        End Sub
    End Class
    If that is not your taste then you would need to use Excel automation which requires more code than SpreadSheetLight does and SpreadSheetLight does not need to have Excel installed.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 11, 2017 2:06 PM