Thread: FoxPro Some General Questions/Import from Excel

Import from Excel
LOCAL lcExcelFile, loExcel
lcExcelFile = GETFILE("XLS")
IF NOT EMPTY(lcExcelFile)
   loExcel = CREATOBJECT("Excel.Application")
   loExcel.Open(lcExcelFile)
   CREATE CURSOR MyExcel (.........) && Field List of the cursor you want to import
   WITH loExcel.ActiveWorkBook
        FOR nRow = 1 TO .Rows.Count
            APPEND BLANK IN MyExcel
            FOR nCol = 1 TO .Columns.Count
                lcField = FIELDS(nCol)
                REPLACE (lcField) WITH .Cells(nRow, nCol).Value  && With proper transformations if you needed one
            NEXT
        NEXT
   ENDWITH
ENDIF



* Need xlConstants.h or explicitly define used xl* constants
#include "xlConstants.h"

USE home()+"samples\data\customer"
Application.datatoclip("customer",reccount(),3)
oExcel = createobject("Excel.application")
WITH oExcel
  .WorkBooks.Add
  WITH .ActiveSheet
    .Range("D9").select
    .Paste
    .Range("A1").select
    .Range("A1").SpecialCells(xlLastCell).CurrentRegion.Select

    lcStartRow = .Application.selection.rows(1).Address(.f.,.f.,xlA1) && D9:P9
    lnStartRow = val(substr(lcStartRow,2))  && 9
    lcNewRow = strtran(lcStartRow, ltrim(str(lnStartRow)), "1") && D1:P1
    lcNextRow = strtran(lcStartRow, ltrim(str(lnStartRow)), "2") && D2:P2
    lcFillRange = substr(lcNextRow,1, at(":",lcNextRow) ) + ;
      substr(lcStartRow, 1, at(":",lcNextRow)-1 )

    .Range(lcStartRow).cut
    .Range(lcNewRow).select
    .Paste && Moved header to row 1
    .Range(lcFillRange).value = "-"  && Fill dummy "-" so included in import
    .SaveAs("c:\temp\myimport.xls",xlExcel5)
  ENDWITH
  .WorkBooks(1).saved = .t.
  .quit
  *   .visible = .t.
ENDWITH
IMPORT from ("c:\temp\myimport.xls") type xl5
BROW