Hi Mike,
in reality, i should allow the user interface cause if the excel file has more than one sheet, i need to allow them to select as many sheet they want to import into a cursor or table, for later to convert it to an SDF, you are correct, i think there is not way to automaticly recognize how many sheets can be in the excel file(normally should be only one) so anyway i will have to allow the users, to select what sheet# in case there are more than one.
here is the whole code
Local lcFile, lcTemp,lcSdf, loExcel As Excel.Application, lnSheet, lnloop, lcOldName, lcNewName, lProceed
lcFile = Getfile("xlsx")
If !Empty(m.lcFile) && check if not cancelled
lcTemp = Addbs(Getenv("TEMP"))+Sys(3)+".xls"
lcSdf = JUSTFNAME(m.lcFile) && This just grab the excel filename when the Getfile() function is issued
lcSdf = Juststem(lcSdf) && Juststem("c:\folder\whatever.XXXX")
loExcel = Createobject("excel.application")
With loExcel
.DisplayAlerts=.F. && somewhat similar to SET SAFETY OFF of VFP
.Workbooks.Open(m.lcFile)
lnSheet = Int(Val(Inputbox("Please select the Target Sheet: 1-"+Transform(.Sheets.Count),"Select Sheet","1")))
If Empty(m.lnSheet) && check if not cancelled
lProceed = .F.
Else
lProceed = .T.
For lnloop = .Sheets.Count To 1 Step -1
If m.lnloop <> m.lnSheet
.Sheets(m.lnloop).Delete
Endif
Next
.ActiveWorkbook.SaveAs(m.lcTemp,39) && Place it in the temp folder
.ActiveWindow.Close(.T.)
.Quit
Endif
Endwith
If m.lProceed && not Cancelled by user?
Import From (m.lcTemp) Type Xls && or XL5
* Rename Field Names to better ones
For lnloop = 1 To Fldcount()
lcOldName = Field(m.lnloop)
lcNewName = Strtran(Alltrim(Transform(&lcOldName)),' ','')
Alter Table Dbf() Rename Column &lcOldName To (m.lcNewName)
Next
Delete
Browse Normal
COPY TO 'f:\BLGFILE\'+m.lcSDF for not delete() type SDF
Close Databases All
* Clean it up, delete temp xl5 file used
Delete File (lcTemp)
Else
Messagebox("Aborted by user!",0+64,"Oppppssss!")
Endif
Else
Messagebox("Aborted by user!",0+64,"Oppppssss!