Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Exporting to Excel
Message
From
15/01/2002 09:00:02
 
 
To
11/01/2002 17:11:45
Robert Ross
Onsite Computer Services
Benton, Tennessee, United States
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00603789
Message ID:
00604802
Views:
19
Robert, I do something similar and use an Excel "template" file. I create a preformatted excel file save it once and always do a SAVEAS to keep it for future use. The following code reads in 92 rows from the VFP supplied customer table that is part of the tastrade database. The cursor is then copied to the Windows clipboard and pasted into Excel at cell "A2" of "Sheet2" in the Excel "template" file created in advance.

Good luck!
LOCAL nRows, nMaxRows, lDataToClip
lDataToClip = .F.

SELECT * FROM tastrade!customer INTO CURSOR ccust
nRows = RECCOUNT() + 1 && include the header

*-- Open Excel
oExcel = CREATEOBJECT("Excel.Application")
WITH oExcel
  *-- Open the reusable Excel template file
  .WorkBooks.Open('d:\temp\excel_tmp.xls')
  *-- How many rows can be written to Excel?
  nMaxRows = .ActiveWorkBook.Activesheet.Rows.Count
  IF nRows < nMaxRows AND nRows <> 1
    .Sheets("Sheet2").Select()
    .Range("A2").Select()
    SELECT ccust
    IF lDataToClip
      *-- create a tab delimited list to the clipboard
      _VFP.DataToClip(,,3)
    ELSE
      *-- This method does not include header info
      FileToStr_ClipText()
    ENDIF
    *-- Paste the data to Excel begining at cell "A2" of "Sheet2"
    .ActiveSheet.Paste()
    *-- Clear out the clipboard
    _ClipText = ""
    *-- Excel's version of SET SAFETY
    .DisplayAlerts = .F.
    *-- Save the file as a new name to keep template in tact.
    .ActiveWorkBook.SaveAs("d:\temp\newsheet.xls")
  ENDIF
  *-- Quit Excel
  .Quit()
ENDWITH

PROCEDURE FileToStr_ClipText()
  lcFile = SYS(2015)+".TXT"
  COPY TO (lcFile) DELIMITED WITH TAB
  _Cliptext = FILETOSTR(lcFile)
  ERASE (lcFile)
ENDPROC
>I have a need to EXPORT a table created by a query to EXCEL and target it to SHEET2
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform