Excel Tablas Pivot importando datos desde dbf

Excel Tablas Pivot importando datos desde dbf

Postby pymsoft » Tue Jun 24, 2008 9:13 am

Alguien hizo algo?



en lo especifico, tengo problemas para transformar este codigo en VB


Code: Select all  Expand view
      With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
      .Connection = Array( _
      "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\lavwin\wp32\tmp\;Mode=Share Deny Write;Extended Proper" _
      , _
      "ties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=18;Jet OLED" _
      , _
      "B:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Passwo" _
      , _
      "rd="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet O" _
      , "LEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False")
      .CommandType = xlCmdTable
      .CommandText = Array("vendiTE")
      .MaintainConnection = True
      .CreatePivotTable TableDestination:="[Cartel3]Foglio1!R3C1", TableName _
      :="Tabella_pivot2", DefaultVersion:=xlPivotTableVersion10
      End With
      With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("MOV_DAT")
      .Orientation = xlRowField
      .Position = 1
      End With
      ActiveSheet.PivotTables("Tabella_pivot2").CalculatedFields.Add "TotaleVendite" _
      , "=MOV_PRZ*MOV_QTA", True
      ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("TotaleVendite"). _
      Orientation = xlDataField
      With ActiveSheet.PivotTables("Tabella_pivot2").PivotFields("MOV_DTMAG")
      .Orientation = xlColumnField
      .Position = 1
      End With




Saludos.
Pedro Gonzalez
User avatar
pymsoft
 
Posts: 383
Joined: Tue Oct 11, 2005 1:01 pm
Location: Savona - Italia

Postby pymsoft » Tue Jun 24, 2008 12:17 pm

Bueno, por si nadie hizo hasta ahora una tabla pivot en excel partiendo de un archivo dbf, aqui mando un ejemplo (deberia ser autocontenido)





Code: Select all  Expand view
********
FUNCTION EsempioPivot()
********
LOCAL oExcel, oHoja, oPivot, cFoglio, oWorkbook, oError, I
LOCAL cFile := CurDrive() + ":\" + CurDir() + "\" + "dati_ven.dbf"
LOCAL aDati := {  { CTOD( "01/01/2008" ), "CATEGORIA 1", 125  },;
                  { CTOD( "05/01/2008" ), "CATEGORIA 2", 132  },;
                  { CTOD( "07/01/2008" ), "CATEGORIA 1", 321  },;
                  { CTOD( "12/01/2008" ), "CATEGORIA 3", 456  },;
                  { CTOD( "21/01/2008" ), "CATEGORIA 1", 654  },;
                  { CTOD( "24/01/2008" ), "CATEGORIA 1", 350  },;
                  { CTOD( "25/01/2008" ), "CATEGORIA 2", 425  },;
                  { CTOD( "26/01/2008" ), "CATEGORIA 1", 310  },;
                  { CTOD( "27/01/2008" ), "CATEGORIA 1", 789  } }
     

    IF !FILE( cFile )
      DBCREATE( cFile, {;
              { "MOV_DAT",   "D",  8, 0 },;
              { "CATEGORIA", "C", 15, 0 },;
              { "VENDUTO"  , "N", 15, 2 } })
      SELECT 0
      USE (cFile) NEW ALIAS "vendite"
      AEVAL( aDati, {|x| vendite->( dbAppend() ), i := 1, AEVAL( x, {|z| vendite->( fieldput( i++, z ) )  } )  } )
      vendite->( dbCloseArea() )
    ENDIF

    TRY
 
      oExcel := TOleAuto():New( "Excel.Application" )
   
      // Excel not available
      if Ole2TxtError() != "S_OK"
        MsgStop("Excel non disponibile!" )
        BREAK
      endif
   
      CursorWait()
     
      oExcel:Visible := .F.
     
      oExcel:WorkBooks:Open( cFile )
      oWorkBook := oExcel:ActiveWorkBook
     
      oPivot := oWorkBook:PivotCaches:Add( 1, "Database" )
      oPivot:CreatePivotTable( "", "Tabella_pivot1", 1 )
     
      oExcel:Sheets:Select(1)
      oHoja := oExcel:Get( "ActiveSheet" )
 
     
      oPivot := oHoja:PivotTables("Tabella_pivot1"):PivotFields("MOV_DAT")
      oPivot:Orientation := 1 // xlRowField
      oPivot:Position := 1
     

      oPivot := oHoja:PivotTables("Tabella_pivot1"):PivotFields("CATEGORIA")
      oPivot:Orientation := 1 // xlRowField
      oPivot:Position := 1 // 1
     
      oPivot := oHoja:PivotTables("Tabella_pivot1"):PivotFields("VENDUTO")
      oPivot:Orientation := 4 // xlDataField
      oPivot:Position := 1     

      oExcel:Sheets(1):Select()
      oHoja := oExcel:Get( "ActiveSheet" )
      oHoja:Range("G1"):Select()

      oHoja:PivotTables("Tabella_pivot1"):Format(3)  // xlReport4

      /* per nascondere elenco di commandi e lista campi     
      oExcel:CommandBars("PivotTable"):Visible := .F.
      oWorkbook:ShowPivotTableFieldList := .F. // ActiveWorkbook.ShowPivotTableFieldList = False
      */
     
      oExcel:Sheets(1):Select() //   LEFT( RIGHT( cFile, 12 ), 8 )
      oHoja := oExcel:Get( "ActiveSheet" )

      // per cancellare il foglio con i dati.
      /*     
      cFoglio := LEFT( RIGHT( cFile, 12 ), 8 )
      cFoglio := STRTRAN( cFoglio, "\", "" )
      oExcel:DisplayAlerts := .F.
      oWorkBook:WorkSheets( cFoglio ):delete()
      oExcel:DisplayAlerts := .T.
      */
 
   
    CATCH oError
   
      MsgStop( oError:Operation+CRLF+oError:Description, APP_NAME )
   
    END TRY
     
         
    oExcel:Visible := .T.
   
    CursorArrow()

RETURN NIL

*
** eof EsempioPivot




Mi problema es el siguiente, bien sabemos que excel soporta hasta 65536 lineas por lo que en el caso que el dbf tenga mas registros, importará solo 65535 lineas mas la primera linea que contiene el nombre de los campos. Para resolver este problema, se inserta el dbf usando OLEDB de excel, pero ahi está el problema que aun no he logrado resolver.


Code: Select all  Expand view
      With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
      .Connection = Array( _
      "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\lavwin\wp32\tmp\;Mode=Share Deny Write;Extended Proper" _
      , _
      "ties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine Type=18;Jet OLED" _
      , _
      "B:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Passwo" _
      , _
      "rd="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet O" _
      , "LEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False")
      .CommandType = xlCmdTable
      .CommandText = Array("vendiTE")
      .MaintainConnection = True
      .CreatePivotTable TableDestination:="[Cartel3]Foglio1!R3C1", TableName _
      :="Tabella_pivot2", DefaultVersion:=xlPivotTableVersion10
      End With



Espero que el ejemplo les sirva, se pueden hacer muy lindos informes con este sistema. :wink:


Saludos
Pedro Gonzalez
User avatar
pymsoft
 
Posts: 383
Joined: Tue Oct 11, 2005 1:01 pm
Location: Savona - Italia


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 123 guests