In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

Guardar o Grabar archivo xls a archivo.txt (Solucionado)

Foro público de Xailer en español
Responder
ryder1912
Mensajes: 29
Registrado: Jue Jul 09, 2015 8:17 pm

Guardar o Grabar archivo xls a archivo.txt (Solucionado)

Mensaje por ryder1912 »

Buenas a todos, tengo una duda de como implementar o programar un procedimiento que me permita grabar un archivo excel y pasarlo a archivo txt
La idea es procecar la hoja de calculo, que luego se ve en browse, y despues grabarlo como txt; Si alguno tiene una idea de como hacerlo, desde ya gracias. Me falta el boton btGrabarClick que no tiene nada.

#include "Xailer.ch"

CLASS TProcesarPagoSueldos FROM TForm

COMPONENT oBevel1
COMPONENT btArchivo
COMPONENT oProg
COMPONENT oLabel1
COMPONENT oLabel2
COMPONENT oBevel2
COMPONENT btProcesar
COMPONENT btGrabar
COMPONENT oTBr
COMPONENT oFileOpenDlg1
COMPONENT oTBrColumn1
COMPONENT oTBrColumn2


METHOD CreateForm()
METHOD btArchivoClick( oSender )
METHOD btProcesaraClick( oSender )
METHOD btGrabarClick( oSender )


ENDCLASS

#include "ImportarPagoSueldosExcel.xfm"

//------------------------------------------------------------------------------

METHOD btArchivoClick( oSender ) CLASS TProcesarPagoSueldos
if ::oFileOpenDlg1:Execute() .and. File(::oFileOpenDlg1:cFullFileName)
::oLabel1:cText:=::oFileOpenDlg1:cFullFileName
::btProcesar:lEnabled:=.T.
else
::oLabel1:cText:=''
::btProcesar:lEnabled:=.F.
endif
RETURN Nil


//------------------------------------------------------------------------------

METHOD btProcesaraClick( oSender ) CLASS TProcesarPagoSueldos
local oExcel, oSheet, lOk, i, cCelda, nMaxRows, aLinea
local nCasa, nCuenta, nCodMov, dFechapago, cIdentEmp, nLegajo, nImporte, cTitular, nTipoDoc, nNroDoc


TRY
oExcel := GetActiveObject( "Excel.Application" )
::btProcesar:lEnabled:=.T.
lOk :=.T.
CATCH
TRY
oExcel := CreateObject('Excel.Application')
lOk:=.T.
CATCH
Alert( "ERROR! Excel not avialable. ["+Ole2TxtError()+"]" )
END
END

if lOk

::btProcesar:lEnabled:=.F.
oExcel:WorkBooks():Open(::oLabel1:cText)
oSheet:=oExcel:Worksheets(1)
oSheet:Calculate()

nMaxRows:=MaximoRowsXLS(oSheet,'A')

::oProg:nValue:=1
::oProg:nMax :=nMaxRows
::oProg:nMin :=1

for i:=1 to nMaxRows

::oProg:StepIt()
if ::oProg:nValue%5==0
ProcessMessages()
endif

cCelda:='A'+AllTrim(Str(i,5,0))
nCasa:=oSheet:Range(cCelda):Value

cCelda:='B'+AllTrim(Str(i,5,0))
nCuenta:=oSheet:Range(cCelda):Value

cCelda:='C'+AllTrim(Str(i,5,0))
nCodMov:=oSheet:Range(cCelda):Value

cCelda:='D'+AllTrim(Str(i,5,0))
dFechapago:=oSheet:Range(cCelda):Value

cCelda:='E'+AllTrim(Str(i,5,0))
cIdentEmp:=oSheet:Range(cCelda):Value

cCelda:='F'+AllTrim(Str(i,5,0))
nLegajo:=oSheet:Range(cCelda):Value

cCelda:='G'+AllTrim(Str(i,5,0))
nImporte:=oSheet:Range(cCelda):Value

cCelda:='H'+AllTrim(Str(i,5,0))
cTitular:=oSheet:Range(cCelda):Value

cCelda:='I'+AllTrim(Str(i,5,0))
nTipoDoc:=oSheet:Range(cCelda):Value

cCelda:='J'+AllTrim(Str(i,5,0))
nNroDoc:=oSheet:Range(cCelda):Value

if !Empty(nCasa)
::oTBr:AddRow({nCasa, nCuenta, nCodMov, dFechapago, cIdentEmp, nLegajo, nImporte, cTitular, nTipoDoc, nNroDoc})
endif

next

oExcel:ActiveWorkbook:Close( .F. )
::btProcesar:lEnabled:=.F.
::btGrabar:lEnabled:=.T.
endif

oExcel := NIL

::oProg:nValue:=0



RETURN Nil

//------------------------------------------------------------------------------

METHOD btGrabarClick( oSender ) CLASS TProcesarPagoSueldos

Return Nil
Última edición por ryder1912 el Vie Nov 20, 2015 1:22 am, editado 1 vez en total.
ryder1912
Mensajes: 29
Registrado: Jue Jul 09, 2015 8:17 pm

Re: Guardar o Grabar archivo xls a archivo.txt

Mensaje por ryder1912 »

Bueno no sé si se entendio bien la ayuda que pido, capaz que es muy dificl de hacerlo pero como pueden ver cuando procesa el archivo.xls lo va recorriendo linea x linea e inserta la fila en un arraybrowse (::oTBr:AddRow...).
La idea es recorrer el archivo.xls o tambien puede ser el arraybrowse y poder grabar los datos en un archivo de texto pero no sé como hacerlo, espero que me puedan ayudar.
ryder1912
Mensajes: 29
Registrado: Jue Jul 09, 2015 8:17 pm

Re: Guardar o Grabar archivo xls a archivo.txt

Mensaje por ryder1912 »

Que "dificl" que era, bueno pude encontrar una solucion, abajo dejo el procedimiento por si alguien le interesa
Como puse arriba la idea era recorrer el excel o el arraybrowse, en este caso hago lo 2do.
Abrir el filesavedlg, crear un archivo asignandole la ruta+nombre, luego recorrer el array y por cada linea, escribir en el fichero los datos del array mas el salto de linea.

METHOD btGuardarClick( oSender ) CLASS TProcesarPagoSueldos
local cFichero, i , linea:=''
if ::oFileSaveDlg1:Execute()

cFichero:= fCreate(::oFileSaveDlg1:cFullFileName)
for i:=1 to Len(::oTBr:aArrayData)

linea:=Str(::oTBr:aArrayData[1],4,0)+' '+Str(::oTBr:aArrayData[2],10,0)+' '+Str(::oTBr:aArrayData[3],3,0)+' '+StrTran(CampoAStr(::oTBr:aArrayData[4]),"'","")+' '
linea+=Str(::oTBr:aArrayData[5],4,0)+' 'Str(::oTBr:aArrayData[6],9,0)+' '+Str(::oTBr:aArrayData[7],13,0)+' '+PadR(::oTBr:aArrayData[8],30)+' ' linea+=Str(::oTBr:aArrayData[9],2,0)+' '+Str(::oTBr:aArrayData[10],8,0)
linea+=Chr(13)+Chr(10)
FWRITE(cFichero,linea)
next

FCLOSE(cFichero)
endif
RETURN Nil
Responder