|
-
May 28th, 2009, 02:54 AM
#1
Thread Starter
Lively Member
Datagrid Value to Excel
this is my last problem in my program... hehehehe.. can someone help me on this. thanks
-
May 28th, 2009, 06:30 PM
#2
Thread Starter
Lively Member
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?
-
May 28th, 2009, 09:50 PM
#3
Junior Member
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
-
May 29th, 2009, 03:32 AM
#4
Thread Starter
Lively Member
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:
Dim xlObject As Excel.Application Dim xlWB As Excel.Workbook Set xlObject = New Excel.Application 'This Adds a new woorkbook, you could open the workbook from file also Set xlWB = xlObject.Workbooks.Add Clipboard.Clear 'Clear the Clipboard With DataGrid1 'Select Full Contents (You could also select partial content) .SelStartCol = 0 .SelEndCol = 4 '==================== i got error on this ==================' Clipboard.SetText .SelText 'Send to Clipboard ' '==================== i got error on this ==================' End With With xlObject.ActiveWorkbook.ActiveSheet .Range("A1").Select 'Select Cell A1 (will paste from here, to different cells) .Paste 'Paste clipboard contents End With ' This makes Excel visible xlObject.Visible = True
original code is >>> Clipboard.SetText .Clip
-
May 29th, 2009, 04:10 AM
#5
Hyperactive Member
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:
Then see if it works.
-
May 29th, 2009, 06:33 PM
#6
Thread Starter
Lively Member
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:
.Paste 'Paste clipboard contents
-
May 29th, 2009, 06:38 PM
#7
Thread Starter
Lively Member
Re: Datagrid Value to Excel
i replace the this error line:
vb Code:
.Paste 'Paste clipboard contents
with this one:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|