Mejorar y optimizar el siguiente código en visual foxpro



Local lcXLSBook as String


lcXLSBook = Getfile('xls, xlsx', 'Archivo:', 'Aceptar', 0, 'Seleccione una hoja de cálculo')
If Empty(lcXLSBook)
	Return .F.
Endif

ExcelToCursor(m.lcXLSBook, "xlsResult2")
*!------------------------------------------------------------------------------
*! Procedure : ExcelToCursor
*! Parametros: pcSrcFile -> Nombre del libro de excel
*!             pcCursorName -> Nombre del cursor
*!------------------------------------------------------------------------------
Procedure ExcelToCursor(pcSrcFile as String, pcCursorName as String)
	
	If Pcount() = 0
		Return .F.
	Else
		If Vartype("pcSrcFile")#"C"
			Return .F.
		Endif

		If !File(pcSrcFile)
			Messagebox("Archivo no encontrado", 16)
			Return .F.
		Endif

		If Vartype("pcCursorName")#"C"
			Return .F.
		Endif
	Endif
	*** Instanciar MS Excel
	Local oExcel as Excel.Application
	m.oExcel = CreateObject("Excel.application")

	If Vartype(oExcel,.T.)!='O'
		Messagebox("No se puede procesar el archivo." +Chr(13) + "Microsoft Excel no está instalado en su ordenador.", 16)
		m.oExcel = Null
		Release oExcel
		Return .F.
	Endif
	*** Abrir archivo de Excel
	m.oExcel.Workbooks.Open(pcSrcFile)
	m.oExcel.Worksheets(1).Activate
	m.oExcel.DisplayAlerts = .F.

	Local oSheet as Object
	m.oSheet = m.oExcel.ActiveSheet

	Local aExcel(1), laStructure(1)
	Local lnCol, lnRow, lnSize, lcCol, lcRow, lcValue, lcCmd
	*** Redimensionar aExcel de acuerdo a las filas y columnas que contiene el libro de excel abierto
	If Evaluate("ALEN(aExcel)") # m.oSheet.UsedRange.Columns.Count
		Dimension aExcel [1, m.oSheet.UsedRange.Columns.Count]
	Endif
	m.lnCol = m.oSheet.UsedRange.Columns.Count
	m.lnRow = m.oSheet.UsedRange.Rows.Count
	*** Pasar los valores del libro de excel a la matriz redimensionada aExcel
	Text to lcCmd TextMerge Noshow Pretext 1+2
		aExcel = m.oExcel.ActiveWorkbook.ActiveSheet.Range(m.oSheet.Cells(1,1), m.oSheet.Cells(<<m.lnRow>>,<<m.lnCol>>)).Value
	Endtext
  	&lcCmd
	*** Cerrar la instancia MS Excel
	m.oExcel.QUIT()
	m.oExcel = Null
	Release oExcel, oSheet
	*** Procedimiento para determinar los tipo de datos por columnas y crear la estructura del cursor
	m.lnRow = Alen(aExcel,1)
	m.lnCol = Iif(Alen(aExcel,2)>0, Alen(aExcel,2), 1)
	*** La matriz laStructure bidimensional
	*** almacena la estructura del cursor
	*** Columna 1 -> Nombre de la columna
	*** Columna 2 -> Tipo de datos
	*** Columna 3 -> Largo
	*** Columna 4 -> Decimal
	*** Columna 5 -> Acepta valores null
	Dimension laStructure(m.lnCol,5)
	For i = 1 to m.lnCol	
		m.lnSize = 1
		m.lcCol = Ltrim(Str(i))
		laStructure(i,1) = aExcel(1,i)
		laStructure(i,2) = Vartype(aExcel(2,i))
		Do Case
			Case laStructure(i,2) = "C" && Character, Memo, Varchar, Varchar (Binary)
				For j = 1 to m.lnRow
					m.lcValue = Iif(m.lnCol = 1, Transform(aExcel(j)), Transform(aExcel(j,i)))
					m.lnSize = Max(m.lnSize, Len(Transform(aExcel(j,i))))
					If At(Chr(13), m.lcValue) > 0
						laStructure(i,2) = "M" && Memo
					Endif
				Endfor

				If laStructure(i,2) = "C"  && Character, Varchar
					*If lnSize < 10
					*	laStructure(i,3) = 10
					*Else
						laStructure(i,3) = lnSize
					*Endif
					laStructure(i,4) = 0
				Else            && Memo, Blob
					laStructure(i,3) = 4
					laStructure(i,4) = 0
				Endif

			Case laStructure(i,2) = "D" OR laStructure(i,2) = "T" && Date, DateTime
				laStructure(i,3) = 8
				laStructure(i,4) = 0

			Case laStructure(i,2) = "L" && Logical
				laStructure(i,3) = 1
				laStructure(i,4) = 0

			Case laStructure(i,2) = "N" && Numeric, Float, Double, o Integer
				laStructure(i,3) = 12
				laStructure(i,4) = 2

			Otherwise
		Endcase
		laStructure(i,5) = .T.
	Endfor
	*** Crear el cursor
	Create Cursor &pcCursorName from array laStructure
	*** Insertar en el cursor los valores desde aExcel
	Local loTherm, lcTask, lnPercent, lnSeconds
	loTherm = NewObject("_thermometer","_therm","","Procesando datos...")
	Local lCellValue
	m.lcRow = ''
	For i = 1 to m.lnRow
		LoTherm.Show()
		lnPercent = i /m.lnRow*100
		Lotherm.Update(lnPercent)
		*SET STEP ON 
		For j = 1 to m.lnCol
			If !Empty(m.lcRow)
				m.lcRow = m.lcRow + ', '
			Endif
			lCellValue = Evaluate([aExcel(i,j)])
			Do Case
				Case Vartype(lCellValue) = "C" && Character, Memo, Varchar, Varchar (Binary)
					SET STEP ON 
					If !Empty(lCellValue) OR lCellValue # ""
						m.lcRow = m.lcRow + ["] + Evaluate([aExcel(i,j)]) + ["]
					Else
						m.lcRow = m.lcRow + [Null]
					Endif
				Case Vartype(lCellValue) = "D" OR Vartype(lCellValue) = "T" && Date, DateTime
					m.lcRow = m.lcRow + [{] + Evaluate([aExcel(i,j)]) + [}]
				Case Vartype(lCellValue) = "N" && Numeric, Float, Double, o Integer
					m.lcRow = m.lcRow + Alltrim(Str(Evaluate([aExcel(i,j)]),12,2))
					*If j<=4
					*	m.lcRow = m.lcRow + ALLTRIM(STR(EVALUATE([aExcel(i,j)])))
					*Else
					*	m.lcRow = m.lcRow + ALLTRIM(STR(EVALUATE([aExcel(i,j)]),12,2))
					*Endif
				Otherwise
					m.lcRow = m.lcRow + Evaluate([aExcel(i,j)])
			Endcase
		Endfor

		If i > 1
			Text to cSQL TextMerge Noshow Pretext 1+2
				Insert Into <<pcCursorName>> Values (<<lcRow>>)
			Endtext
			ExecScript(cSQL)
		Endif
		m.lcRow = ""
	Endfor
	Lotherm.Complete()	&&&& Porcentaje de avance
	*** Liberar variables
	Release pcSrcFile, laStructure, lnSize, lcValue, lcCmd
	Release lCellValue, aExcel, lnCol, lnRow, lcCol, lcRow, cSQL, i, j
	Sele &pcCursorName
	Go top
	*** Retornar el cursor
	Return SetResultSet(pcCursorName)
Endproc