to Mr Nages : Import Excel into Xbrowse

to Mr Nages : Import Excel into Xbrowse

Postby Silvio.Falconi » Mon Nov 16, 2015 9:06 am

Someone have asample to import a excel sheet into xbrowse ?

I saw a sample but not run
Code: Select all  Expand view
#include "fivewin.ch"

    function Main()

       local oRange, aData

       oRange   := GetExcelRange( ExePath() + "Articoli", "ArrayTest","A1:B6"  )
       aData    := ArrTranspose( oRange:Value )
       oRange:WorkSheet:Parent:Close()
       XBrowse( aData )

    return nil

    function ExePath()
       return cFilePath( GetModuleFileName() )
 


I must converte this from A1 to BH50
Image
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6871
Joined: Thu Oct 18, 2012 7:17 pm

Re: to Mr Nages : Import Excel into Xbrowse

Postby Rick Lipkin » Mon Nov 16, 2015 12:47 pm

SIlvo

Consider this code ..

Rick Lipkin
Code: Select all  Expand view

//------------------------------------
Static Func _DoIt(oRsInvt )

LOCAL SITEDBF, DBF_STRU
LOCAL oEXCEL,nROW, nROWS,nCOL,nCOLS, oBOOK, xVALUE
LOCAL nREC
LOCAL oDLG1,oLBX1,oBTN1,oBTN2
LOCAL cALIAS

cSAY := "Creating Temporary files"
oSAY:ReFresh()
SysReFresh()

DO WHILE .T.
   SITEDBF := "TEMP"+(SUBSTR(TIME(),7,2)+SUBSTR(TIME(),4,2))+".DBF"
   IF .not. FILE( xVOL+"\DBTMP\"+SITEDBF )
      EXIT
   ENDIF
ENDDO

DBF_STRU := { }
AADD( DBF_STRU, { "
AGENCY",       "C",   5,  0 } )
AADD( DBF_STRU, { "
USER",         "C",  30,  0 } )
AADD( DBF_STRU, { "
PHONE",        "C",  10,  0 } )
AADD( DBF_STRU, { "
DECAL",        "C",   7,  0 } )
AADD( DBF_STRU, { "
TYPE",         "C",  35,  0 } )
AADD( DBF_STRU, { "
SERNO",        "C",  20,  0 } )
AADD( DBF_STRU, { "
CLASSIF",      "N",  15,  0 } )
AADD( DBF_STRU, { "
MODELYR",      "N",   7,  0 } )
AADD( DBF_STRU, { "
MAKE",         "C",  10,  0 } )
AADD( DBF_STRU, { "
MANUFACT",     "C",  10,  0 } )
AADD( DBF_STRU, { "
STREET",       "C",  10,  0 } )
AADD( DBF_STRU, { "
CITY",         "C",  10,  0 } )
AADD( DBF_STRU, { "
COST",         "N",  10,  2 } )
AADD( DBF_STRU, { "
ACQUIRED",     "N",   7,  0 } )
AADD( DBF_STRU, { "
PURCHASE",     "C",  10,  0 } )
AADD( DBF_STRU, { "
ROOM",         "C",   7,  0 } )
AADD( DBF_STRU, { "
SECTION",      "C",   8,  0 } )
AADD( DBF_STRU, { "
DATE_RCVD",    "D",   8,  0 } )


DBCREATE( xVOL+"
\DBTMP\"+SITEDBF, DBF_STRU )

SELECT 9
IF NETUSE( xVOL+"
\DBTMP\"+SITEDBF, .T.,5)
ELSE
   SELECT( GOBACK )
   _Cleanup()
   RETURN(.F.)
ENDIF

nREC := 0

cSAY := "
Opening Excel File "+STR(nREC)
oSAY:ReFresh()
SysReFresh()

oEXCEL := TOleAuto():New( "
Excel.Application" )
oExcel:Workbooks:Open( cPATH )

oBook := oExcel:Get( "
ActiveSheet" )
nROWS := oBook:UsedRange:Rows:Count()
nCOLS := oBook:UsedRange:Columns:Count()

SELECT 9
FOR nROW := 2 to nROWS
   APPEND BLANK
   FOR nCOL := 1 to 15 //nCOLS
      xVALUE := oBook:Cells( nROW, nCOL):Value
      FieldPut( nCol,xValue )

      nREC++
      cSAY := "
Opening Excel File "+STR(nREC)
      oSAY:ReFresh()
      SysReFresh()

   NEXT
NEXT

SELECT 9
dbCommit()

oExcel:Quit()

SELECT 9
GO TOP

xBrowse()

CLose databases

Return(.t.)
User avatar
Rick Lipkin
 
Posts: 2642
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: to Mr Nages : Import Excel into Xbrowse

Postby FranciscoA » Mon Nov 16, 2015 2:03 pm

Rick.
Thanks for sharing your code.
Regards.
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2134
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: to Mr Nages : Import Excel into Xbrowse

Postby joseluisysturiz » Mon Nov 16, 2015 4:00 pm

Esta es la forma en que lo hago desde Excel a Mysql, espero les sirva , saludos... :shock:

Code: Select all  Expand view

PROCEDURE excel_tabla( oMeter, oText, oDlg, lEnd, cFileXls )

   LOCAL oExcel, oBook, nRows, nCols, nCol, nRow, oError, cSql

   oExcel := TOleAuto():New( "Excel.Application" ) // ACTIVANDO EXCEL
   oExcel:Workbooks:Open( cFileXls ) // ABRO EL ARCHIVO SELECCIONADO
//
   oBook := oExcel:Get( "ActiveSheet" ) // ACTIVO HOJA EXCEL
   nRows := oBook:UsedRange:Rows:Count() // CANTIDAD LINEAS EXCEL INCLUYENDO LA LINEA 1 QUE ES LA CABECERA
   nCols := oBook:UsedRange:Columns:Count() // CANTIDAD COLUMNAS EXCEL

   oMeter:nTotal = nRows // PASO AL METER TOTAL DE LINEAS

   FOR nRow = 2 TO nRows // CARGANDO DE EXCEL TABLA REPORTES
      cSql := "INSERT INTO servicios_reportes SET "
//
      cSql += "rep_nombre_cliente='" + oBook:Cells( nRow, 1 ):Value + "', "
      cSql += "rep_num_reporte=" + ClipValue2SQL( oBook:Cells( nRow, 2 ):Value ) + ", "
      cSql += "rep_equipo_cliente=" + ClipValue2SQL( oBook:Cells( nRow, 3 ):Value ) + ", "
// ojo - esta columna esta llegando de tipo numerica
      cSql += "rep_num_serial=" + ClipValue2SQL( SUBST( cValToChar( oBook:Cells( nRow, 4 ):Value ), 1, 10 ) ) + ", " // 18/12/2014
//
      cSql += "rep_num_llamada=" + ClipValue2SQL( oBook:Cells( nRow, 5 ):Value ) + ", "
      cSql += "rep_preventivo=" + ClipValue2SQL( 1 ) + ", " // SIEMPRE ES PREVENTIVO - COL.6 DE EXCEL
//
      cSql += "rep_tiempo_viaje_inicio=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 7 ):Value, 1, 5 ) ) + ", "
      cSql += "rep_tiempo_viaje_fin=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 7 ):Value, 7, 5 ) ) + ", "
//
      cSql += "rep_tiempo_espera_inicio=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 8 ):Value, 1, 5 ) ) + ", "
      cSql += "rep_tiempo_espera_fin=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 8 ):Value, 7, 5 ) ) + ", "
//
      cSql += "rep_tiempo_rep_inicio=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 9 ):Value, 1, 5 ) ) + ", "
      cSql += "rep_tiempo_rep_fin=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 9 ):Value, 7, 5 ) ) + ", "
//
      cSql += "rep_localidad=" + ClipValue2SQL( AllTrim( oBook:Cells( nRow, 10 ):Value ) +; // COL.10 y 11
         ", " + AllTrim( oBook:Cells( nRow, 11 ):Value ) ) + ", "

      cSql += "rep_estado=" + ClipValue2SQL( AllTrim( oBook:Cells( nRow, 11 ):Value ) ) + ", " // 05/01/2015

      cSql += "rep_codi_ingeniero=" + ClipValue2SQL( oBook:Cells( nRow, 12 ):Value ) + ", "
//
      IF LEN( oBook:Cells( nRow, 13 ):Value ) <= 50 // DESDE 1-50
         cSql += "rep_observa1=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 13 ):Value ,;
            1, Len( oBook:Cells( nRow, 13 ):Value ) ) ) + ", "

      ELSE
         cSql += "rep_observa1=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 13 ):Value, 1, 50 ) ) + ", "
      ENDIF

      IF LEN( oBook:Cells( nRow, 13 ):Value ) > 50 .and. LEN( oBook:Cells( nRow, 13 ):Value ) <= 110 // DESDE 51-110
         cSql += "rep_observa2=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 13 ):Value ,;
            51, Len( oBook:Cells( nRow, 13 ):Value ) ) ) + ", "

      ELSE // DESDE 111-170
         cSql += "rep_observa2=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 13 ):Value, 51, 110 ) ) + ", "
         cSql += "rep_observa3=" + ClipValue2SQL( SUBST( oBook:Cells( nRow, 13 ):Value ,;
            111, Len( oBook:Cells( nRow, 13 ):Value ) ) ) + ", "
      ENDIF
//
      cSql += "rep_np1=" + ClipValue2SQL( IF( Len( cValToChar( oBook:Cells( nRow, 14 ):Value ) ) > 0 ,;
         SUBST( cValToChar( oBook:Cells( nRow, 14 ):Value ), 1, 12 ), ) ) + ", "
      cSql += "rep_np2=" + ClipValue2SQL( IF( Len( cValToChar( oBook:Cells( nRow, 15 ):Value ) ) > 0 ,;
         SUBST( cValToChar( oBook:Cells( nRow, 15 ):Value ), 1, 12 ), ) ) + ", "
//
*** COL 16 - FECHA DE IMPRESION
cSql += "rep_fch_impresion='" + oBook:Cells( nRow, 16 ):Value + "', "
*** COL 17 - TIPO DE ZONA
cSql += "rep_tipo_zona='" + oBook:Cells( nRow, 17 ):Value + "', "
*** COL 18 - SERVICIO
cSql += "rep_servicio='" + oBook:Cells( nRow, 18 ):Value + "', "
*** COL 19 - TARIFA
cSql += "rep_tarifa=" + ClipValue2SQL( oBook:Cells( nRow, 19 ):Value ) + ", "
//
      cSql += "rep_fch_emision=" + ClipValue2SQL( Date() ) + ","
// AUDITORIA
      cSql += "usuario=" + ClipValue2SQL( "00" ) + ","
      cSql += "hora=" + ClipValue2SQL( time() ) + ","
      cSql += "fchcrea=" + ClipValue2SQL( date() )

      TRY
         oDatos:oConex:Execute( cSql )

      CATCH oError
         MSGALERT( oError:Description + CRLF + ;
            "Error Grabando en Tabla servicios_reportes, Sentencia" + CRLF + CRLF + ;
            cSql, oDatos:cTitMsg )

      END

      oMeter:Set( nRow )

   NEXT

// CERRANDO EXCEL
   oExcel:WorkBooks:Close()
   oExcel:Application:Quit()
   oExcel:Quit()
   oExcel := Nil

   MsgInfo( cValToChar( nRows -1 ) + " Reportes Importados Correctamente", oDatos:cTitMsg )

RETURN
 
Dios no está muerto...

Gracias a mi Dios ante todo!
User avatar
joseluisysturiz
 
Posts: 2064
Joined: Fri Jan 06, 2006 9:28 pm
Location: Guatire - Caracas - Venezuela

Re: to Mr Nages : Import Excel into Xbrowse

Postby FranciscoA » Mon Nov 16, 2015 7:37 pm

Jose Luis.
Thanks
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2134
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: to Mr Nages : Import Excel into Xbrowse

Postby Silvio.Falconi » Tue Nov 17, 2015 10:29 am

But why not exist a general function ?
I must make a Prg for each dbf ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6871
Joined: Thu Oct 18, 2012 7:17 pm

Re: to Mr Nages : Import Excel into Xbrowse

Postby joseluisysturiz » Tue Nov 17, 2015 12:53 pm

Silvio.Falconi wrote:But why not exist a general function ?
I must make a Prg for each dbf ?


Buen dia, no se si existe una funcion, pero podrias leyendo la hoja de excel, crearte un array con las cabezeras de las columnas, y basado en ello crear las dbf con sus campos por dicho array y luego hacer la importacion, tambien puedes crear un array con los nombres de los libros y leerlos uno por uno, como es mi caso, cualquier duda estoy a la orden, saludos... :shock:
Last edited by joseluisysturiz on Tue Nov 17, 2015 2:04 pm, edited 1 time in total.
Dios no está muerto...

Gracias a mi Dios ante todo!
User avatar
joseluisysturiz
 
Posts: 2064
Joined: Fri Jan 06, 2006 9:28 pm
Location: Guatire - Caracas - Venezuela

Re: to Mr Nages : Import Excel into Xbrowse

Postby Euclides » Tue Nov 17, 2015 1:54 pm

Hi to all!
You know what? The funniest thing is: SILVIO´S ROUTINE DOES WORK!
Only have to put the right xls and worksheet names.
Thanks for the info!
It is an excellent general purpose routine.
;-) Euclides
User avatar
Euclides
 
Posts: 155
Joined: Wed Mar 28, 2007 1:19 pm

Re: to Mr Nages : Import Excel into Xbrowse

Postby joseluisysturiz » Tue Nov 17, 2015 6:40 pm

Euclides wrote:Hi to all!
You know what? The funniest thing is: SILVIO´S ROUTINE DOES WORK!
Only have to put the right xls and worksheet names.
Thanks for the info!
It is an excellent general purpose routine.
;-) Euclides


viewtopic.php?f=3&t=31574

IMPORTANTE
viewtopic.php?f=3&t=29989&p=170683&hilit=FW_ExcelToDBF#p170683
Dios no está muerto...

Gracias a mi Dios ante todo!
User avatar
joseluisysturiz
 
Posts: 2064
Joined: Fri Jan 06, 2006 9:28 pm
Location: Guatire - Caracas - Venezuela

Re: to Mr Nages : Import Excel into Xbrowse

Postby nageswaragunupudi » Thu Nov 19, 2015 11:44 pm

1) We can read Excel Range into an Array and browse.
Mr Silvio:
The sample you posted should work. Please check if your paramters are correct or not.
2) We can *directly* browse an Excel Range in XBrowse and even make changes to Excel sheet through XBrowse.
Code: Select all  Expand view

   oRange := GetExcelRange( cExcelFile, [cSheet], [cRange] )
   @ r,c, XBROWSE oBrw SIZE w,h PIXEL OF oWnd DATASOURCE oRange AUTOCOLS
   oBrw:CreateFromCode()
 


3) We can also import a range from Excel to DBF with function
Code: Select all  Expand view

   oRange := GetExcelRange( cExcelFile, [cSheet], [cRange] )
   FW_ExcelToDbf( oRange, cFieldList, lHasHeaders, bProgress )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10353
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: to Mr Nages : Import Excel into Xbrowse

Postby nageswaragunupudi » Fri Nov 20, 2015 1:22 am

Mr Jose Luis

I suggest simplification and general function
Code: Select all  Expand view

#xtranslate SQL INSERT INTO <tbl> ( <cols,...> ) ARRAY <a> => ;
"INSERT INTO " + <(tbl)> + " ( " +  FW_QuotedColSQL( #<cols> ) + " ) " + " VALUES " + ;
 StrTran( StrTran( FW_ValToSql( AClone( <a> ) ), '( (', '(' ), ') )', ')' )

#xtranslate SQL INSERT INTO <tbl> ARRAY <a> => ;
"INSERT INTO " + <(tbl)> + " VALUES " + ;
 StrTran( StrTran( FW_ValToSql( AClone( <a> ) ), '( (', '(' ), ') )', ')' )
// note: These translates are available in FWH15.10
//------------------------------------

  oRange := GetExcelRange( cExcelFile, [cSheet], [cRange] )
  aData  := ArrTranspose( oRange:Value )
  cSql := INSERT INTO MyTable ARRAY aData
  oCn:Execute( cSql )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10353
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: to Mr Nages : Import Excel into Xbrowse

Postby Silvio.Falconi » Fri Nov 20, 2015 8:49 am

Mr Nages,
Code: Select all  Expand view


#include "fivewin.ch"

Function test()
    Local oRange,lOpened:=.f.
      oRange   := GetExcelRange( ExePath() + "PRODOTTI.xls" )

    USE ARTICOLI ALIAS AR
    SELECT AR

    FW_ExcelToDBF( oRange, nil, .t. )

    xbrowse()

     oRange   := NIL

return nil

function ExePath()
return cFilePath( GetModuleFileName() )



here run ok but ....How can you do to keep the existing values on Dbf when importing empty cells?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 6871
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 42 guests