Alejandro Garcia G.
unread,Aug 4, 2011, 5:43:16 PM8/4/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Comunidad de Visual Foxpro en Español
Utilizo esta funcion que encontre en algun lado, sirve para exportar
una tabla a Excel, el detalle es que al pasarme los datos numericos me
quedan como texto ya que me deja el punto decimal (.00) y no lo deseo
asi.
¿Se puede modificar, que puedo modificar?. Gracias.
********************************************************************
*!* FUNCTION Exp2Excel( [cCursor, [cFileSave, [cTitulo]]] )
*!*
*!* Exporta un Cursor de Visual FoxPro a Excel, utilizando la
*!* técnica de importación de datos externos en modo texto.
*!*
*!* PARAMETROS OPCIONALES:
*!* - cCursor Alias del cursor que se va a exportar.
*!* Si no se informa, utiliza el alias
*!* en que se encuentra.
*!*
*!* - cFileName Nombre del archivo que se va a grabar.
*!* Si no se informa, muestra el libro generado
*!* una vez concluída la exportación.
*!*
*!* - cTitulo Titulo del informe. Si se informa, este
*!* ocuparía la primera file de cada hoja del libro.
********************************************************************
Function Exp2Excel( cCursor, cFileSave, cTitulo )
Local cWarning
cWarning = "Exportar a EXCEL"
If Empty(cCursor)
cCursor = Alias()
Endif
If Type('cCursor') # 'C' Or !Used(cCursor)
Messagebox("Parámetros Inválidos",16,cWarning)
Return .F.
Endif
*********************************
*** Creación del Objeto Excel ***
*********************************
Wait Window 'Abriendo aplicación Excel.' Nowait Noclear
oExcel = Createobject("Excel.Application")
Wait Clear
If Type('oExcel') # 'O'
Messagebox("No se puede procesar el archivo porque no tiene la
aplicación" ;
+ Chr(13) + "Microsoft Excel instalada en su computador.",
16,cWarning)
Return .F.
Endif
oExcel.VISIBLE = .t.
oExcel.workbooks.Add
Local lnRecno, lnPos, lnPag, lnCuantos, lnRowTit, lnRowPos, i,
lnHojas, cDefault
cDefault = Addbs(Sys(5) + Sys(2003))
Select (cCursor)
lnRecno = Recno(cCursor)
Go Top
*************************************************
*** Verifica la cantidad de hojas necesarias ***
*** en el libro para la cantidad de datos ***
*************************************************
lnHojas = Round(Reccount(cCursor)/65000,0)
Do While oExcel.Sheets.Count < lnHojas
oExcel.Sheets.Add
Enddo
lnPos = 0
lnPag = 0
Do While lnPos < Reccount(cCursor)
lnPag = lnPag + 1 && Hoja que se está procesando
Wait Windows 'Exportando cursor ' + Upper(cCursor) + ' a Microsoft
Excel...' ;
+ Chr(13) + '(Hoja ' + Alltrim(Str(lnPag)) + ' de ' +
Alltrim(Str(lnHojas)) ;
+ ')' Noclear Nowait
If File(cDefault + cCursor + ".txt")
Delete File (cDefault + cCursor + ".txt")
Endif
Copy Next 65000 To (cDefault + cCursor + ".txt") Delimited With
Character ";"
lnPos = Recno(cCursor)
oExcel.Sheets(lnPag).Select
XLSheet = oExcel.ActiveSheet
XLSheet.Name = cCursor + '_' + Alltrim(Str(lnPag))
lnCuantos = Afields(aCampos,cCursor)
********************************************************
*** Coloca título del informe (si este es informado) ***
********************************************************
If !Empty(cTitulo)
XLSheet.Cells(1,1).Font.Name = "Arial"
XLSheet.Cells(1,1).Font.Size = 12
XLSheet.Cells(1,1).Font.BOLD = .T.
XLSheet.Cells(1,1).Value = cTitulo
XLSheet.Range(XLSheet.Cells(1,1),XLSheet.Cells(1,lnCuantos)).MergeCells
= .T.
XLSheet.Range(XLSheet.Cells(1,1),XLSheet.Cells(1,lnCuantos)).Merge
XLSheet.Range(XLSheet.Cells(1,1),XLSheet.Cells(1,lnCuantos)).HorizontalAlignment
= 3
lnRowPos = 3
Else
lnRowPos = 2
Endif
lnRowTit = lnRowPos - 1
**********************************
*** Coloca títulos de Columnas ***
**********************************
For i = 1 To lnCuantos
lcName = aCampos(i,1)
lcCampo = Alltrim(cCursor) + '.' + aCampos(i,1)
XLSheet.Cells(lnRowTit,i).Value=lcName
XLSheet.Cells(lnRowTit,i).Font.BOLD = .T.
XLSheet.Cells(lnRowTit,i).Interior.ColorIndex = 15
XLSheet.Cells(lnRowTit,i).Interior.Pattern = 1
XLSheet.Range(XLSheet.Cells(lnRowTit,i),XLSheet.Cells(lnRowTit,i)).BorderAround(7)
Next
XLSheet.Range(XLSheet.Cells(lnRowTit,
1),XLSheet.Cells(lnRowTit,lnCuantos)).HorizontalAlignment = 3
*************************
*** Cuerpo de la hoja ***
*************************
oConnection = XLSheet.QueryTables.Add("TEXT;" + cDefault +
cCursor + ".txt", ;
XLSheet.Range("A" + Alltrim(Str(lnRowPos))))
With oConnection
.Name = cCursor
.FieldNames = .T.
.RowNumbers = .F.
.FillAdjacentFormulas = .F.
.PreserveFormatting = .T.
.RefreshOnFileOpen = .F.
.RefreshStyle = 1 && xlInsertDeleteCells
.SavePassword = .F.
.SaveData = .T.
.AdjustColumnWidth = .T.
.RefreshPeriod = 0
.TextFilePromptOnRefresh = .F.
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = 1 && xlDelimited
.TextFileTextQualifier = 1 && xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = .F.
.TextFileTabDelimiter = .F.
.TextFileSemicolonDelimiter = .T.
.TextFileCommaDelimiter = .F.
.TextFileSpaceDelimiter = .F.
.TextFileTrailingMinusNumbers = .T.
.Refresh
Endwith
XLSheet.Range(XLSheet.Cells(lnRowTit,
1),XLSheet.Cells(XLSheet.Rows.Count,lnCuantos)).Font.Name = "Arial"
XLSheet.Range(XLSheet.Cells(lnRowTit,
1),XLSheet.Cells(XLSheet.Rows.Count,lnCuantos)).Font.Size = 8
XLSheet.Columns.AutoFit
XLSheet.Cells(lnRowPos,1).Select
oExcel.ActiveWindow.FreezePanes = .T.
Wait Clear
Enddo
oExcel.Sheets(1).Select
oExcel.Cells(lnRowPos,1).Select
If !Empty(cFileSave)
oExcel.DisplayAlerts = .F.
oExcel.ActiveWorkbook.SaveAs(cFileSave)
oExcel.Quit
Else
oExcel.Visible = .T.
Endif
*!* Go lnRecno
Release oExcel,XLSheet,oConnection
If File(cDefault + cCursor + ".txt")
Delete File (cDefault + cCursor + ".txt")
Endif
Return .T.
Endfunc