Results 1 to 2 of 2

Thread: Datagrid to excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    90

    Datagrid to excel

    hello im having a problem with transferring datagrid to excel this is what i have
    Code:
    Dim xlApp As Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            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 = xlWorkBook.Sheets("sheet1")
            For i = 0 To DataGridView1.RowCount - 2
                For j = 0 To DataGridView1.ColumnCount - 1
                    If DataGridView1.Columns(j).Visible = True Then
                        xlWorkSheet.Cells(i + 1, j + 1) = _
                            DataGridView1(j, i).Value.ToString()
                    End If
                Next
            Next
            xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
            xlWorkBook.Close()
            xlApp.Quit()
    some of the columns in my datagrid are hidden, in the middle of the code theres if statement so it will copy the columns that are visible, but my problem is when i open the excel file some of the columns are empty.
    PL:VB2008
    LEVEL: beginner

    There must be a simpler way to do this...

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Datagrid to excel

    Go to my signature , and click working with excel.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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