Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Transferring data from Excel to a VFP table
Message
From
20/11/2002 05:52:51
 
 
To
19/11/2002 18:24:33
Henry Ravichander
RC Management Systems Inc.
Saskatchewan, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00724558
Message ID:
00724662
Views:
18
>Hi All:
>
>I have transferred data from an Access Table into Excel. I would now like to transfer the data from Excel to VFP tables.
>
>Concern: I want specific fields from Excel to to be inserted into specific fields in the VFP Table. Note that the field names in the VFP table differ from that in Excel.
>
>What is the best way to accomplish this? i.e., should I just append all the data from Excel into a VFP table just created to hold all the data from this table and then copy specific fields to the master VFP table?
>
>Thank you for your suggestions.
Here is what I do:
1) I sql select the data into a cursor named c1
2) I create an instance of excel and put some values into some cells like this:

lcExcelFolder=ADDBS(SYS(5)+SYS(2003)+"\ExcelTemplates")
lcExcelFileTempletName=lcExcelFolder+"5YRExcelTemp.XLS"
THISFORM.Outputfile="5YR_"+TRANSFORM(YEAR(DATE()))+TRANSFORM(MONTH(DATE()))+TRANSFORM(DAY(DATE()))+".XLS"
lcExcelFileName=lcExcelFolder+JUSTFNAME(THISFORM.Outputfile)
lcExcelOutputFolder=ADDBS(SYS(5)+SYS(2003)) +"ExcelDocs\"

THISFORM.oExcel=CREATEOBJECT("Excel.Application")
IF FILE(lcExcelFileTempletName)
THISFORM.oWorkBook=THISFORM.oExcel.workbooks.OPEN(lcExcelFileTempletName)
ELSE
THISFORM.oWorkBook=THISFORM.oExcel.workbooks.ADD()
ENDIF
IF FILE(lcExcelOutputFolder+THISFORM.Outputfile)
ERASE (lcExcelOutputFolder+THISFORM.Outputfile)
ENDIF
THISFORM.oWorkBook.SAVEAS(lcExcelOutputFolder+THISFORM.Outputfile)

*THISFORM.drEditBox1.VALUE=THISFORM.drEditBox1.VALUE+CHR(13)+CHR(10)+"XLS Name: "+lcExcelOutputFolder+THISFORM.Outputfile

THISFORM.oExcel.worksheets("Title").ACTIVATE
oRange=THISFORM.oExcel.activesheet.RANGE("a1:n5550")
oRange.Cells(6,14).VALUE=THISFORM.drcombobox1.DISPLAYVALUE+" "+TRANSFORM(THISFORM.drTextbox1.VALUE)

oRange.Cells(20,2).VALUE="Annual "+TRANSFORM(cYR5)+"-"+TRANSFORM(cYR1)
oRange.Cells(6,14).VALUE="December "+TRANSFORM(cYR1)

THISFORM.oExcel.DisplayAlerts=.F.


3) I do something like this:
*************************************************************************************
**** EXCEL ************************************************************************
*************************************************************************************
lnRecCount=RECCOUNT('c1')
IF lnRecCount>0
LOCATE
_VFP.DATATOCLIP(,,3) && copy all the data to the clipboard

THISFORM.oExcel.worksheets(3).ACTIVATE && activate sheet 3 in this case
WITH THISFORM.oExcel.activesheet
oRange=.RANGE("a1:a1")
.cells(1, 1).VALUE = cMHd

oRange=.RANGE("a3:a3")
oRange.SELECT()
.PASTE
.Rows(3).Delete

* Sum up values
.cells(14, 1).VALUE="Grand Total"
.cells(14, 1).FONT.Bold = .T.

.cells(14, 3).Formula = "=SUM(C3:C11)"
.cells(14, 3).FONT.Bold = .T.


.cells(14, 5).Formula = "=SUM(e3:e11)"
.cells(14, 5).FONT.Bold = .T.


.cells(14, 7).Formula = "=SUM(g3:g11)"
.cells(14, 7).FONT.Bold = .T.


.cells(14, 9).Formula = "=SUM(i3:i11)"
.cells(14, 9).FONT.Bold = .T.


.cells(14, 11).Formula = "=SUM(k3:k11)"
.cells(14, 11).FONT.Bold = .T.

*calculate percentages
.cells(14, 2).Formula = "=if(C14<>0,(c14-K14)/k14,0)"
.cells(14, 2).FONT.Bold = .T.
.cells(14, 4).Formula = "=if(E14<>0,(c14-E14)/E14,0)"
.cells(14, 4).FONT.Bold = .T.
.cells(14, 6).Formula = "=if(G14<>0,(E14-G14)/G14,0)"
.cells(14, 6).FONT.Bold = .T.

.cells(14, 8).Formula = "=if(I14<>0,(G14-I14)/I14,0)"
.cells(14, 8).FONT.Bold = .T.
.cells(14, 10).Formula = "=if(K14<>0,(I14-K14)/K14,0)"
.cells(14, 10).FONT.Bold = .T.


ENDWITH
ENDIF


I hope this will help you a little

Peter
Peter Cortiel
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform