*-- Program pentru exportul listei de view-uri din Oracle in XLSX
*-- Foloseste ExcelXML pentru crearea fisierelor Excel fara automatizare
*-- Presupune ca exista gnHandle (connection handle) si goExecutor cu metoda oExecuta
Local lcOutputFile, lcOutputPath, llSucces

llSucces = .F.
lcOutputPath = GETDIR("C:\","Alegeti directorul pentru salvarea exportului","Director salvare export",64)
IF EMPTY(m.lcOutputPath)
	RETURN m.llSucces
ENDIF 
lcOutputFile = m.lcOutputPath + "roa_export.xlsx"
llSucces = ExportOracleViewsToXLSX("vact,vbal,vbalana,vbalanta_parteneri,vireg_parteneri,vjv2013,vjc2013", m.lcOutputFile)
	
RETURN m.llSucces 	
*-----------------------------------------------------------
*-- Export direct in XLSX folosind ExcelXML
*-----------------------------------------------------------
Procedure ExportOracleViewsToXLSX
	Parameters pcCursorName, pcOutputFile
	Local lcOutputFile, llSuccess, loExcelXML
	Local lnRecCount, lcLog, lcLogLevel

	*-- Initializeaza log-ul
	lcLog = ""
	lcLogLevel = "INFO"

	*-- Verifica daca cursorul exista sau foloseste lista existenta
	If Empty(pcCursorName)
		pcCursorName = "curViews"  && Presupune ca ai deja o lista de view-uri
	Endif

	lcLog = lcLog + "[" + Time() + "] Verificare cursor: " + pcCursorName + Chr(13) + Chr(10)

	If !Used(pcCursorName)
		lcLog = lcLog + "[" + Time() + "] EROARE: Cursorul '" + pcCursorName + "' nu exista!" + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Asigura-te ca ai o lista de view-uri incarcata." + Chr(13) + Chr(10)
		lcLogLevel = "ERROR"
		MessageBox(lcLog, 16, "Log Export - Eroare")
		Return .F.
	Endif

	*-- Selecteaza cursorul si verifica datele
	Select (pcCursorName)
	lnRecCount = Reccount()
	lcLog = lcLog + "[" + Time() + "] Cursor selectat. Inregistrari gasite: " + Transform(lnRecCount) + Chr(13) + Chr(10)

	If lnRecCount = 0
		lcLog = lcLog + "[" + Time() + "] AVERTISMENT: Nu exista inregistrari de exportat." + Chr(13) + Chr(10)
		lcLogLevel = "WARNING"
		AMessageBox(lcLog, 64, "Log Export - Avertisment")
		Return .T.
	Endif

	*-- Determina fisierul de iesire
	If Empty(pcOutputFile)
		lcOutputFile = Putfile("Salveaza lista view-urilor Oracle", "oracle_views.xlsx", "XLSX")
		If Empty(lcOutputFile)
			lcLog = lcLog + "[" + Time() + "] Export anulat de utilizator." + Chr(13) + Chr(10)
			AMessageBox(lcLog, 64, "Log Export - Anulat")
			Return .F.
		Endif
	Else
		lcOutputFile = pcOutputFile
	Endif

	*-- Asigura-te ca extensia este .xlsx
	If Upper(Right(lcOutputFile, 4)) <> "XLSX"
		lcOutputFile = Forceext(lcOutputFile, "xlsx")
	Endif

	lcLog = lcLog + "[" + Time() + "] Fisier destinatie: " + lcOutputFile + Chr(13) + Chr(10)

	Try
		lcLog = lcLog + "[" + Time() + "] Creare obiect ExcelXML..." + Chr(13) + Chr(10)

		*-- Creeaza obiectul ExcelXML
		loExcelXML = Createobject("ExcelXML")

		If Type("loExcelXML") <> "O"
			lcLog = lcLog + "[" + Time() + "] EROARE: Nu s-a putut crea obiectul ExcelXML!" + Chr(13) + Chr(10)
			lcLog = lcLog + "[" + Time() + "] Verifica daca ExcelXML este inclus corect in proiect." + Chr(13) + Chr(10)
			lcLogLevel = "ERROR"
			AMessageBox(lcLog, 16, "Log Export - Eroare")
			Return .F.
		Endif

		lcLog = lcLog + "[" + Time() + "] Obiect ExcelXML creat cu succes." + Chr(13) + Chr(10)

		*-- Configureaza proprietatile pentru formatare
		lcLog = lcLog + "[" + Time() + "] Configurare proprietati ExcelXML..." + Chr(13) + Chr(10)

		With loExcelXML
			.ColumnHeaderBackgroundColor = "DDDDDD"  && Gri deschis pentru header
			.ColumnHeaderForeColor = "000000"        && Text negru
			.WorksheetName = "Oracle Views"          && Numele foii de lucru
			.Author = "Visual FoxPro Export"         && Autorul documentului
		Endwith

		*-- Exporta cursorul la Excel XML
		Select (pcCursorName)
		Go Top

		lcLog = lcLog + "[" + Time() + "] Incepere export la XML..." + Chr(13) + Chr(10)

		*-- Creeaza fisierul XML temporar
		Local lcTempXmlFile
		lcTempXmlFile = Forceext(lcOutputFile, "xml")

		*-- Exporta la XML cu header
		loExcelXML.TableToExcel(lcTempXmlFile, "", .T.)  && .T. pentru a include header-ul

		lcLog = lcLog + "[" + Time() + "] Export XML completat: " + lcTempXmlFile + Chr(13) + Chr(10)

		*-- Converteste XML la XLSX
		lcLog = lcLog + "[" + Time() + "] Conversie XML la XLSX..." + Chr(13) + Chr(10)

		*-- Parametrii: (Filename, FileFormat, OpenAfterExporting)
		*-- FileFormat: 51 = xlOpenXMLWorkbook (XLSX)
		loExcelXML.ConvertXmlToXlsx(lcTempXmlFile, 51, .F.)

		*-- Muta fisierul XLSX la locatia dorita
		Local lcGeneratedXlsx
		lcGeneratedXlsx = Forceext(lcTempXmlFile, "xlsx")

		If File(lcGeneratedXlsx) And lcGeneratedXlsx <> lcOutputFile
			lcLog = lcLog + "[" + Time() + "] Mutare fisier de la " + lcGeneratedXlsx + " la " + lcOutputFile + Chr(13) + Chr(10)
			Copy File (lcGeneratedXlsx) To (lcOutputFile)
			Delete File (lcGeneratedXlsx)
		Endif

		*-- Sterge fisierul XML temporar
		If File(lcTempXmlFile)
			lcLog = lcLog + "[" + Time() + "] Stergere fisier temporar XML." + Chr(13) + Chr(10)
			Delete File (lcTempXmlFile)
		Endif

		lcLog = lcLog + "[" + Time() + "] Export XLSX completat cu succes!" + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Fisier final: " + lcOutputFile + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Total inregistrari exportate: " + Transform(lnRecCount) + Chr(13) + Chr(10)

		llSuccess = .T.
		lcLogLevel = "SUCCESS"

	Catch To loError
		lcLog = lcLog + "[" + Time() + "] EROARE CRITICA in export!" + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Mesaj: " + loError.Message + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Linia: " + Transform(loError.Lineno) + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Procedura: " + loError.Procedure + Chr(13) + Chr(10)
		llSuccess = .F.
		lcLogLevel = "ERROR"

	Finally
		*-- Cleanup
		If Type("loExcelXML") = "O"
			lcLog = lcLog + "[" + Time() + "] Cleanup obiect ExcelXML." + Chr(13) + Chr(10)
			loExcelXML = Null
		Endif

	Endtry

	*-- Afiseaza log-ul final
	Local lnIcon
	Do Case
		Case lcLogLevel = "ERROR"
			lnIcon = 16
		Case lcLogLevel = "WARNING"
			lnIcon = 48
		Case lcLogLevel = "SUCCESS"
			lnIcon = 64
		Otherwise
			lnIcon = 64
	Endcase

	AMessageBox(lcLog, lnIcon, "Log Export Oracle Views - " + lcLogLevel)

	*-- Intreaba daca vrea sa deschida fisierul doar daca exportul a reusit
	If llSuccess And File(lcOutputFile)
		If AMessageBox("Vrei sa deschizi fisierul exportat?", 36, "Deschide fisierul") = 6
			Run /N EXPLORER "&lcOutputFile"
		Endif
	Endif

	Return llSuccess
Endproc

*-----------------------------------------------------------
*-- Export direct in XML (daca nu vrei XLSX)
*-----------------------------------------------------------
Procedure ExportOracleViewsToXML
	Parameters pcCursorName, pcOutputFile
	Local lcOutputFile, llSuccess, loExcelXML
	Local lcLog, lcLogLevel

	*-- Initializeaza log-ul
	lcLog = ""
	lcLogLevel = "INFO"

	If Empty(pcCursorName)
		pcCursorName = "curViews"
	Endif

	lcLog = lcLog + "[" + Time() + "] Verificare cursor pentru export XML: " + pcCursorName + Chr(13) + Chr(10)

	If !Used(pcCursorName)
		lcLog = lcLog + "[" + Time() + "] EROARE: Cursorul '" + pcCursorName + "' nu exista!" + Chr(13) + Chr(10)
		lcLogLevel = "ERROR"
		AMessageBox(lcLog, 16, "Log Export XML - Eroare")
		Return .F.
	Endif

	Select (pcCursorName)
	Local lnRecCount
	lnRecCount = Reccount()
	lcLog = lcLog + "[" + Time() + "] Inregistrari gasite: " + Transform(lnRecCount) + Chr(13) + Chr(10)

	If lnRecCount = 0
		lcLog = lcLog + "[" + Time() + "] AVERTISMENT: Nu exista inregistrari de exportat." + Chr(13) + Chr(10)
		lcLogLevel = "WARNING"
		AMessageBox(lcLog, 64, "Log Export XML - Avertisment")
		Return .T.
	Endif

	If Empty(pcOutputFile)
		lcOutputFile = Putfile("Salveaza in Excel XML", "oracle_views.xml", "XML")
		If Empty(lcOutputFile)
			lcLog = lcLog + "[" + Time() + "] Export XML anulat de utilizator." + Chr(13) + Chr(10)
			AMessageBox(lcLog, 64, "Log Export XML - Anulat")
			Return .F.
		Endif
	Else
		lcOutputFile = pcOutputFile
	Endif

	lcLog = lcLog + "[" + Time() + "] Fisier destinatie XML: " + lcOutputFile + Chr(13) + Chr(10)

	Try
		lcLog = lcLog + "[" + Time() + "] Creare obiect ExcelXML pentru export XML..." + Chr(13) + Chr(10)

		loExcelXML = Createobject("ExcelXML")

		If Type("loExcelXML") <> "O"
			lcLog = lcLog + "[" + Time() + "] EROARE: Nu s-a putut crea obiectul ExcelXML!" + Chr(13) + Chr(10)
			lcLogLevel = "ERROR"
			AMessageBox(lcLog, 16, "Log Export XML - Eroare")
			Return .F.
		Endif

		*-- Configureaza proprietatile
		lcLog = lcLog + "[" + Time() + "] Configurare proprietati pentru XML..." + Chr(13) + Chr(10)

		With loExcelXML
			.ColumnHeaderBackgroundColor = "CCCCCC"
			.ColumnHeaderForeColor = "000000"
			.WorksheetName = "Oracle Views"
			.Author = "Visual FoxPro Export"
		Endwith

		*-- Exporta cursorul
		Select (pcCursorName)
		Go Top

		lcLog = lcLog + "[" + Time() + "] Incepere export la fisier XML..." + Chr(13) + Chr(10)

		loExcelXML.TableToExcel(lcOutputFile, "", .T.)  && .T. pentru a include header-ul

		lcLog = lcLog + "[" + Time() + "] Export XML completat cu succes!" + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Fisier creat: " + lcOutputFile + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Total inregistrari exportate: " + Transform(lnRecCount) + Chr(13) + Chr(10)

		llSuccess = .T.
		lcLogLevel = "SUCCESS"

	Catch To loError
		lcLog = lcLog + "[" + Time() + "] EROARE la exportul XML!" + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Mesaj: " + loError.Message + Chr(13) + Chr(10)
		lcLog = lcLog + "[" + Time() + "] Linia: " + Transform(loError.Lineno) + Chr(13) + Chr(10)
		llSuccess = .F.
		lcLogLevel = "ERROR"

	Finally
		If Type("loExcelXML") = "O"
			lcLog = lcLog + "[" + Time() + "] Cleanup obiect ExcelXML." + Chr(13) + Chr(10)
			loExcelXML = Null
		Endif

	Endtry

	*-- Afiseaza log-ul final
	Local lnIcon
	Do Case
		Case lcLogLevel = "ERROR"
			lnIcon = 16
		Case lcLogLevel = "WARNING"
			lnIcon = 48
		Case lcLogLevel = "SUCCESS"
			lnIcon = 64
		Otherwise
			lnIcon = 64
	Endcase

	AMessageBox(lcLog, lnIcon, "Log Export XML - " + lcLogLevel)

	Return llSuccess
Endproc

*-----------------------------------------------------------
*-- Exemplu de utilizare cu lista ta existenta de view-uri
*-----------------------------------------------------------
Procedure TestExportViews
	Local llSuccess

	*-- Presupune ca ai deja un cursor cu view-urile incarcat
	*-- Exemplu: ai un cursor numit "myViews" cu campurile:
	*-- owner, view_name, text_length, view_type, last_analyzed

	*-- Test export XLSX
	llSuccess = ExportOracleViewsToXLSX("myViews", "C:\temp\test_views.xlsx")
	If llSuccess
		? "Export XLSX reusit!"
	Else
		? "Export XLSX esuat!"
	Endif

	*-- Test export XML
	llSuccess = ExportOracleViewsToXML("myViews", "C:\temp\test_views.xml")
	If llSuccess
		? "Export XML reusit!"
	Else
		? "Export XML esuat!"
	Endif

Endproc "O"
lcLog = lcLog + "[" + Time() + "] Cleanup obiect ExcelXML." + Chr(13) + Chr(10)
loExcelXML = Null
Endif

Endtry

*-- Afiseaza log-ul final
Local lnIcon
Do Case
	Case lcLogLevel = "ERROR"
		lnIcon = 16
	Case lcLogLevel = "WARNING"
		lnIcon = 48
	Case lcLogLevel = "SUCCESS"
		lnIcon = 64
	Otherwise
		lnIcon = 64
Endcase

AMessageBox(lcLog, lnIcon, "Log Export XML - " + lcLogLevel)

Return llSuccess
Endproc

*-----------------------------------------------------------
*-- Exemplu de utilizare cu lista ta existenta de view-uri
*-----------------------------------------------------------
Procedure TestExportViews
	Local llSuccess

	*-- Presupune ca ai deja un cursor cu view-urile incarcat
	*-- Exemplu: ai un cursor numit "myViews" cu campurile:
	*-- owner, view_name, text_length, view_type, last_analyzed

	*-- Test export XLSX
	llSuccess = ExportOracleViewsToXLSX("myViews", "C:\temp\test_views.xlsx")
	If llSuccess
		? "Export XLSX reusit!"
	Else
		? "Export XLSX esuat!"
	Endif

	*-- Test export XML
	llSuccess = ExportOracleViewsToXML("myViews", "C:\temp\test_views.xml")
	If llSuccess
		? "Export XML reusit!"
	Else
		? "Export XML esuat!"
	Endif

Endproc "O"
lcLog = lcLog + "[" + Time() + "] Cleanup obiect ExcelXML." + Chr(13) + Chr(10)
loExcelXML = Null
Endif

Endtry

*-- Afi?eaza log-ul final
Local lnIcon
Do Case
	Case lcLogLevel = "ERROR"
		lnIcon = 16
	Case lcLogLevel = "WARNING"
		lnIcon = 48
	Case lcLogLevel = "SUCCESS"
		lnIcon = 64
	Otherwise
		lnIcon = 64
Endcase

AMessageBox(lcLog, lnIcon, "Log Export XML - " + lcLogLevel)

Return llSuccess
Endproc

*-----------------------------------------------------------
*-- Exemplu de utilizare cu lista ta existenta de view-uri
*-----------------------------------------------------------
Procedure TestExportViews
	Local llSuccess

	*-- Presupune ca ai deja un cursor cu view-urile īncarcat
	*-- Exemplu: ai un cursor numit "myViews" cu cāmpurile:
	*-- owner, view_name, text_length, view_type, last_analyzed

	*-- Test export XLSX
	llSuccess = ExportOracleViewsToXLSX("myViews", "C:\temp\test_views.xlsx")
	If llSuccess
		? "Export XLSX reu?it!"
	Else
		? "Export XLSX e?uat!"
	Endif

	*!*		*-- Test export XML
	*!*		llSuccess = ExportOracleViewsToXML("myViews", "C:\temp\test_views.xml")
	*!*		If llSuccess
	*!*			? "Export XML reusit!"
	*!*		Else
	*!*			? "Export XML esuat!"
	*!*		Endif

Endproc
   