Results 1 to 8 of 8

Thread: Excel and Datagridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Excel and Datagridview

    Hi All

    The end of my current project is in site.

    However, one thing still to sort.

    I have a datagridview which I would like to be able to export the information to an Excel spreadsheet, and be able to reload the information later on from a spceified worksheet.

    I've found a few "solutions" online, but none of them seem to work and so I need some major help.

    My level of programming is very, very basic and so please forgive any ignorance which may happen throughout this!

    Also, I'd like to avoid using Databases with Access etc because I am aware that some of the machines I'll be deploying the programme on do not have Access installed and would not want to have to install it either.

    Thanks for your help

    M

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Excel and Datagridview

    What do you mean "reload the information later on from a specified worksheet?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Excel and Datagridview

    basically, save the information to that spreadsheet, and should the programme with the DGV in it be closed for whatever reason, be able to select the worksheet to reload to continue working on it ..

  4. #4
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Excel and Datagridview

    open it from excel??
    Here is some code that will create an excel sheet for you, may have to tweak it a bit but it should get you there


    Code:
    Dim misValue As Object = System.Reflection.Missing.Value
            Dim i As Integer
            Dim j As Integer
    
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = CType(xlWorkBook.Sheets("Sheet1"), Excel.Worksheet)
    
            For Each col As DataGridViewColumn In DataGridView1.Columns
    
                xlWorkSheet.Cells.Columns.CurrentRegion.Columns(col.Index + 1) = col.HeaderText.ToString
            Next
            For i = 0 To DataGridView1.RowCount - 2
                For j = 0 To DataGridView1.ColumnCount - 1
                    If worker.CancellationPending Then
                        e.Cancel = True
                        Exit For
                    End If
                    xlWorkSheet.Cells(i + 2, j + 1) = _
                        DataGridView1(j, i).Value.ToString()
    
                      Next
            Next
    
                xlWorkBook.Close()
                xlApp.Quit()
                releaseObject(xlApp)
                releaseObject(xlWorkBook)
                releaseObject(xlWorkSheet)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: Excel and Datagridview

    Thanks for this! Getting some blue underlines saying "declaration expected" ...sorry for my lack of knowledge :S

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Excel and Datagridview

    You might consider not dealing with Excel at all. Excel can open XML files easily, Excel can also write XML files easily, and that will be much easier for exporting and importing.
    My usual boring signature: Nothing

  7. #7
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Excel and Datagridview

    Quote Originally Posted by MacShand View Post
    Thanks for this! Getting some blue underlines saying "declaration expected" ...sorry for my lack of knowledge :S
    Would help if you gave more information

    but you probably need these place under you Public class form

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

  8. #8
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Excel and Datagridview

    just go to this thread & find the signature "Export DGV to word ~ excel " in .pouls signature.

    this is a good note
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width