ADO com Excel
Posted: Sun Jan 28, 2007 12:06 pm
Ola amigos,
Preciso ler planilha Excel com ADO, é possivel ?
gostaria de um exemplo
Ari
Preciso ler planilha Excel com ADO, é possivel ?
gostaria de um exemplo
Ari
www.FiveTechSoft.com
https://fivetechsoft.com/forums/
Code: Select all
oExcel := TOleAuto():New( "Excel.Application" )
oExcel:WorkBooks:Open("yourfile.xls")
oHoja := oExcel:Get( "ActiveSheet" )
nRows := oHoja:UsedRange:Rows:Count()
nCols := oHoja:UsedRange:Columns:Count()
USE filedbf
FOR nRow := 2 TO nRows
APPEND BLANK
For nfrom := 1 TO nCols
FIELDPUT(nFrom, oHoja:Cells(nRow,nCol):Value)
NEXT
NEXT
Code: Select all
Function Teste(oMeter) // Ler arquivos Excel com ODBC
local oDbc := TOdbc():New() // criar o ODBC no Windows !
local n := 1
local oAdo := TdbOdbc():New("SELECT * FROM [folha1$]", oDbc)
oMeter:nTotal := oAdo:RecCount()
While !oAdo:eof()
++n
oMeter:cText := padr(oAdo:FieldGet(1),18) + " " + padr( oAdo:FieldGet(2) ,20)
oMeter:Set(n)
oMeter:refresh()
oAdo:skip()
End
return nil
Code: Select all
*
* ----------------------------------------------------------------
*
FUNCTION OLEExcel()
LOCAL oWnd, oMenu
MENU oMenu
MENUITEM "&Probar Excel y DBF"
MENU
MENUITEM "&Leer SIGLAS.XLS" ACTION LEER()
MENUITEM "&Enviar SIGLAS.DBF a Excel" ACTION ENVIAR() WHEN FILE( "SIGLAS.DBF" )
SEPARATOR
MENUITEM "&Salir" ACTION oWnd:End()
ENDMENU
ENDMENU
DEFINE WINDOW oWnd FROM 0,0 TO 20,70 MENU oMenu TITLE "Probar Excel y DBF"
ACTIVATE WINDOW oWnd MAXIMIZED
RETURN
FUNCTION LEER()
LOCAL oExcel, oHoja, nRows, nCols
LOCAL aCampos:={}, nRow, nCol
oExcel := TOleAuto():New( "Excel.Application" )
oExcel:WorkBooks:Open(SFN2LFN(cFilePath(GetModuleFileName(GetInstance()))+"SIGLAS.xls"))
oHoja := oExcel:Get( "ActiveSheet" )
nRows := oHoja:UsedRange:Rows:Count()
nCols := oHoja:UsedRange:Columns:Count()
FOR nCol := 1 TO nCols
IF ValType( oHoja:Cells( 2, nCol ):Value ) = "C"
AADD( aCampos, { oHoja:Cells( 1, nCol ):Value, "C", 80, 0 } )
ELSEIF ValType( oHoja:Cells( 2, nCol ):Value ) = "N"
AADD( aCampos, { oHoja:Cells( 1, nCol ):Value, "N", 15, 4 } )
ELSEIF ValType( oHoja:Cells( 2, nCol ):Value ) = "L"
AADD( aCampos, { oHoja:Cells( 1, nCol ):Value, "L", 1, 0 } )
ELSEIF ValType( oHoja:Cells( 2, nCol ):Value ) = "D"
AADD( aCampos, { oHoja:Cells( 1, nCol ):Value, "D", 8, 0 } )
ENDIF
NEXT
DBCREATE( "EXCEL", aCampos )
USE "EXCEL" NEW
FOR nRow := 2 TO nRows
APPEND BLANK
FOR nCol := 1 TO nCols
FIELDPUT( nCol, oHoja:Cells( nRow, nCol ):Value )
NEXT
NEXT
CLOSE DATABASES
oExcel:Quit()
oHoja:End()
oExcel:End()
MsgInfo( "Foi criado o arquivo EXCEL.DBF" )
RETURN
FUNCTION ENVIAR()
LOCAL oExcel, oHoja
LOCAL nRow := 1, nCol
oExcel := TOleAuto():New( "Excel.Application" )
oExcel:WorkBooks:Add()
oHoja := oExcel:Get( "ActiveSheet" )
USE "SIGLAS" NEW
FOR nCol := 1 TO FCOUNT()
oHoja:Cells( nRow, nCol ):Value := FieldName( nCol )
NEXT
DO WHILE .NOT. EOF()
nRow++
FOR nCol := 1 TO FCOUNT()
oHoja:Cells( nRow, nCol ):Value := FieldGet( nCol )
NEXT
SKIP
ENDDO
FOR nCol := 1 TO FCOUNT()
oHoja:Columns( nCol ):AutoFit()
NEXT
CLOSE DATABASES
oExcel:Visible := .T.
oHoja:End()
oExcel:End()
RETURN