Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Code for excel model
Message
From
11/11/2005 08:30:14
 
 
To
10/11/2005 11:39:08
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01067207
Message ID:
01067602
Views:
18
LOCAL oExcel, oWorkbook, oSheet, nRowNumber, nColumnNumber

nRowNumber = 1
nColumnNumber = 1
oExcel = createObject("Excel.Application")

with oExcel
    .displayAlerts = .f.
    .ignoreRemoteRequests = .t.	&& Will help w/ automation 
                                && while excel is in use by 
                                && user for other purposes
    .sheetsInNewWorkbook = 1
    oWorkbook = .workbooks.add()
    .visible = .t.   && For the sake of demo.  I usually make it .f.
endwith

oSheet = oWorkbook.sheets(1)

&& Fill with data, option 1.  Not pretty output, but fast.
select yourData
go top in yourData
application.datatoclip(,,3)
oSheet.paste( oSheet.cells(1,1) )
**** END OPTION 1 ****

&& Fill with data, option 2.  As pretty as you want it, but slower.
with oSheet
    select yourData
    go top in yourData
    scan
        nColumnNumber = 1
        with .cells(nRowNumber, nColumnNumber)
            .font.bold = .t.
            .value = yourData.field1
        endwith
        nColumnNumber = nColumnNumber + 1
        with .cells(nRowNumber, nColumnNumber)
            .interior.colorIndex = 15
            .value = yourData.field2
        endwith
        nColumnNumber = nColumnNumber + 1
        
        with .cells(nRowNumber, nColumnNumber)
            .borders(9).weight = 2       && Bottom border on cell
            .value = yourData.field3
        endwith
        nRowNumber = nRowNumber + 1
    endscan
endwith
**** END OPTION 2 ****

with oWorkbook
    .saveas("c:\test.xls")
    .close()
endwith
with oExcel
    .ignoreRemoteRequests = .f.
    .quit()
endwith

&& Paranoia cleanup
oSheet = .NULL.
oWorkbook = .NULL.
oExcel = .NULL.
release oSheet
release oWorkbook
release oExcel


********** END CODE
To get all of the specifics of how to do the different formatting tricks in Excel, I like to go into Excel, hit Alt + F11 to get the VBA editor to come up, and then hit F2 to see the Object Browser. From there, you can browse every object, property, method... available to you in the Excel object model. It can be kind of hard to know what you're looking for sometimes when you're new though, so it often helps to just record a macro of the task you want to automate, view the macro when you're done, and do your best to translate the VBA to FoxPro code. There are some syntax differences you will need to overcome in the translation. Oh, and if you just run the code above, options 1 & 2 will both be executed, and you don't really want that. Pick the method that best suits your desired output and run with that.

Best of luck to you.

Paul
Paul A. Busbey
Victoria Insurance
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform