PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Datagrid Value to Excel-VBForums
Results 1 to 7 of 7

Thread: Datagrid Value to Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Datagrid Value to Excel

    this is my last problem in my program... hehehehe.. can someone help me on this. thanks

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Re: Datagrid Value to Excel

    BTW.. the data's in datagrid is not all from database/recordset.. i have 2 columns coming from a textbox.. is it possible to move all the data's in my datagrid to an excel sheet?

  3. #3
    Junior Member wysiwyg327's Avatar
    Join Date
    May 2009
    Location
    App.Path
    Posts
    19

    Re: Datagrid Value to Excel

    Try to use this as a reference :

    Code:
    Private Sub TransFer_Xls()
    
    Dim xlApp As Excel.Application
    Dim xlsource As Excel.Workbook
    Dim wkSheetSrc As Excel.Worksheet
    Dim cellCtr As Long
    Dim rst As ADODB.Recordset
    Dim FileXls As String
    Set rst = New ADODB.Recordset
    
    rst.CursorLocation = adUseClient
    
    
    If Not xlApp Is Nothing Then Set xlApp = Nothing
    
    Set xlApp = New Excel.Application
    Set xlsource = xlApp.Workbooks.Open(App.Path & "\excel_file.xls")
    Set wkSheetSrc = xlsource.Worksheets(1)
    
    
    'use this if you want to save the xls to another filename
    FileXls = App.Path & "\file.xls"
    xlApp.ActiveWorkbook.SaveAs FileName
    'end
    xlApp.DisplayAlerts = False
    xlApp.Visible = True
    
    'sql query
    strSql = "Select * From......"
    
    rst.Open strSql, cnn, 3, 3
    
    On Error Resume Next
    If Not rst.EOF Then
        Do While Not rst.EOF
            With wkSheetSrc
                .Cells(cellCtr, 1).Value = rst!FieldTest 'sample field
            End With
            rst.MoveNext
            cellCtr = cellCtr + 1
        DoEvents
        Loop
    End If
    
    xlApp.ActiveWorkbook.Save
    xlApp.ActiveWorkbook.Close
    xlApp.Quit
    Set xlApp = Nothing
    
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Re: Datagrid Value to Excel

    thanks for the reply.. i found a code for mshflex then i apply in datagrid.. i got 1 error.. hope someone can figure it out...
    vb Code:
    1. Dim xlObject    As Excel.Application
    2.  
    3.       Dim xlWB        As Excel.Workbook
    4.  
    5.           Set xlObject = New Excel.Application
    6.  
    7.           'This Adds a new woorkbook, you could open the workbook from file also
    8.  
    9.           Set xlWB = xlObject.Workbooks.Add
    10.  
    11.           Clipboard.Clear 'Clear the Clipboard
    12.  
    13.           With DataGrid1
    14.  
    15.               'Select Full Contents (You could also select partial content)
    16.  
    17.               .SelStartCol = 0
    18.               .SelEndCol = 4
    19.           '==================== i got error on this =================='
    20.               Clipboard.SetText .SelText  'Send to Clipboard          '
    21.           '==================== i got error on this =================='
    22.           End With
    23.  
    24.           With xlObject.ActiveWorkbook.ActiveSheet
    25.  
    26.               .Range("A1").Select 'Select Cell A1 (will paste from here, to different cells)
    27.            
    28.               .Paste              'Paste clipboard contents
    29.  
    30.           End With
    31.  
    32.           ' This makes Excel visible
    33.  
    34.           xlObject.Visible = True


    original code is >>> Clipboard.SetText .Clip

  5. #5
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    278

    Re: Datagrid Value to Excel

    I think it is throwing up the error because it needs to be saved into the clipboard as an object, not as text. Replace the error line with:

    vb Code:
    1. SendKeys "^c"

    Then see if it works.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Re: Datagrid Value to Excel

    i replace the error line with your code.. i got error "Paste method of Worksheet class failed"

    here's the error code:

    vb Code:
    1. .Paste             'Paste clipboard contents

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    66

    Re: Datagrid Value to Excel

    i replace the this error line:
    vb Code:
    1. .Paste             'Paste clipboard contents

    with this one:

    vb Code:
    1. SendKeys "^v"

    i got empty excel sheet..

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width