Export Datagrid data to CSV

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnlim20088
    New Member
    • Jan 2007
    • 74

    #1

    Export Datagrid data to CSV

    Hi All,

    I does have a question about export datagrid data to csv. Well, I know the common solution is

    1) select the data again and put in the datatable, then export to csv.

    BUT I don't want this, I want DIRECTLY export what I saw in datagrid to CSV, because some of the value I changed in database before I display in the datagrid,
    does anyone can help me on this?
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by johnlim20088
    Hi All,

    I does have a question about export datagrid data to csv. Well, I know the common solution is

    1) select the data again and put in the datatable, then export to csv.

    BUT I don't want this, I want DIRECTLY export what I saw in datagrid to CSV, because some of the value I changed in database before I display in the datagrid,
    does anyone can help me on this?

    You need VB code or C# code

    VB code

    Public Sub create_csv(flx As MSHFlexGrid)
    Dim str1 As String
    Open App.Path & "\export.cs v" For Output As #1 ' Open file for output.
    For i = 0 To flx.Rows - 1
    str1 = ""
    For j = 0 To flx.Cols - 1
    str1 = str1 & flx.TextMatrix( i, j) & " "
    If j <> flx.Cols - 1 Then str1 = str1 & ","
    Next
    Print #1, str1
    Next
    Close #1
    'Open the excel
    openfile (flx.Cols)
    End Sub

    Public Sub openfile(thecol s As Integer)
    Dim objExcel As New Excel.Applicati on
    Dim objWorkBooks As Object
    Dim objAsheet As Object
    'On Error Resume Next
    If Err.Number Then
    Err.Clear
    Set objExcel = CreateObject("E xcel.Applicatio n")
    End If
    Set objWorkBooks = objExcel.Workbo oks
    objWorkBooks.Op en App.Path & "\export.cs v", , , 2, , , , , ","

    objExcel.Visibl e = True

    For intcol = 1 To thecols
    'objExcel.Colum ns(intCol).Auto Fit()
    objExcel.Column s(intcol).AutoF ormat (2)
    objExcel.Range( "a1", (objExcel.Colum ns(thecols).Add ressLocal) & TheRows).AutoFo rmat (GridStyle)
    Next
    End Sub

    use data Grid for Flex grid

    Comment

    Working...