elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Tablas Dinamicas .Net pivot Tables
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Tablas Dinamicas .Net pivot Tables  (Leído 6,769 veces)
rafram

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Tablas Dinamicas .Net pivot Tables
« en: 30 Diciembre 2010, 18:36 pm »

Hola Todos, estuve buscando informacion de como crear una tabla dinamica en Excel desde . net encontre varios tutos y programas pero no eran muy entendibles, des pues de mucho trabajo lo logre sacar y aki lo adjunto para su ayuda

se importan las dll, se toma como base la informacion que se encuentra en un archivo excel, esto puede variar de hecho la creacion del archivo la hice tambien en .net pero para efectos de la creacion de la tabla dinamica tome desde aki para que fuera mas rapido y no tuviera que estar creando el archivo cada vez que queria crear la tabla, asi que lo primero es contar con la informacion.

Código
  1. Imports System.IO
  2. Imports Microsoft.Office.Interop
  3. Imports System.Diagnostics
  4. Imports System.Runtime.InteropServices
  5.  
  6. Public Sub Dinamic3()
  7.  
  8.        Dim xlapp As New Excel.Application
  9.        Dim xlBook As Excel.Workbook
  10.        Dim xlSheet As Excel.Worksheet
  11.        Dim xlSheet1 As Excel.Worksheet
  12.        Dim PRange As Excel.Range
  13.        Dim FinalRow As Long
  14.        Dim FinalCol As Long
  15.  
  16.        Try
  17.            ' Modify the File Name as necessary
  18.            xlapp.Workbooks.Open("C:\Informe Diario\InforDiario-20101111.xls")
  19.  
  20.            xlBook = xlapp.ActiveWorkbook()
  21.  
  22.            xlSheet1 = xlapp.ActiveSheet()
  23.            FinalRow = xlSheet1.Cells(xlSheet1.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
  24.            FinalCol = xlSheet1.Cells(1, xlSheet1.Columns.Count).End(Excel.XlDirection.xlToLeft).Column
  25.            PRange = xlSheet1.Cells(1, 1).Resize(FinalRow, FinalCol)
  26.  
  27.  
  28.            'Creo una hoja y la coloco al inicio
  29.            xlSheet = xlapp.Worksheets.Add(Before:=xlapp.Worksheets("Datos"))
  30.            'xlSheet.Visible = True
  31.            xlSheet.Activate()
  32.            xlSheet.Name = "Tablaaaa"
  33.  
  34.            xlBook.ActiveSheet.Cells(3, 1).Select()
  35.  
  36.            'creo la Tabla Dinamica
  37.            xlBook.PivotCaches.Create(Excel.XlPivotTableSourceType.xlDatabase, SourceData:="Datos!R1C1:R65536C10", _
  38.                          Version:=Excel.XlPivotTableVersionList.xlPivotTableVersion10).CreatePivotTable _
  39.                          (TableDestination:=xlSheet.Range("A1:D25"), TableName:="PivotTable1", _
  40.                           DefaultVersion:=Excel.XlPivotTableVersionList.xlPivotTableVersion10)
  41.  
  42.  
  43.            With xlSheet.PivotTables("PivotTable1").PivotFields("Convenio")
  44.                .Orientation = Excel.XlPivotFieldOrientation.xlPageField
  45.                .Position = 1
  46.            End With
  47.            With xlSheet.PivotTables("PivotTable1").PivotFields("Servicio")
  48.                .Orientation = Excel.XlPivotFieldOrientation.xlPageField
  49.                .Position = 1
  50.            End With
  51.            With xlSheet.PivotTables("PivotTable1").PivotFields("Etapa")
  52.                .Orientation = Excel.XlPivotFieldOrientation.xlRowField
  53.                .Position = 1
  54.            End With
  55.  
  56.            xlSheet.PageSetup.Zoom = 90
  57.  
  58.            xlSheet.PivotTables("PivotTable1").AddDataField(xlSheet.PivotTables _
  59.                                                            ("PivotTable1").PivotFields("Valor"), _
  60.                                                            "Cuenta de Valor", Excel.XlConsolidationFunction.xlCount)
  61.  
  62.            xlSheet.PivotTables("PivotTable1").AddDataField(xlSheet.PivotTables _
  63.                                                            ("PivotTable1").PivotFields("Dias"), _
  64.                                                            "Cuenta de Dias", Excel.XlConsolidationFunction.xlCount)
  65.  
  66.            With xlSheet.PivotTables("PivotTable1").DataPivotField
  67.                .Orientation = Excel.XlPivotFieldOrientation.xlColumnField
  68.                .Position = 1
  69.            End With
  70.            With xlSheet.PivotTables("PivotTable1").PivotFields("Cuenta de Dias")
  71.                .Caption = "Promedio de Dias"
  72.                .Function = Excel.XlConsolidationFunction.xlAverage
  73.                .NumberFormat = "0"
  74.            End With
  75.            xlSheet.PivotTables("PivotTable1").AddDataField(xlSheet.PivotTables _
  76.                                                                ("PivotTable1").PivotFields("Valor"), _
  77.                                                                "Cuenta de Valor2", Excel.XlConsolidationFunction.xlCount)
  78.  
  79.            With xlSheet.PivotTables("PivotTable1").PivotFields("Cuenta de Valor2")
  80.                .Caption = "Suma de Valorrrr"
  81.                .Function = Excel.XlConsolidationFunction.xlSum
  82.                .NumberFormat = "$ #.##0"
  83.            End With
  84.  
  85.            Dim pvi As Excel.PivotItem
  86.            With xlSheet.PivotTables("PivotTable1").PivotFields("Etapa")
  87.                For Each pvi In .PivotItems
  88.                    If pvi.Name = "11 Exportacion" Or _
  89.                    pvi.Name = "12 Espera RIPS" Or _
  90.                    pvi.Name = "13 Transmitir" Or _
  91.                    pvi.Name = "14 Impresion" Or _
  92.                    pvi.Name = "15 Entregado a Armado" Or _
  93.                    pvi.Name = "16 Esperar Radicación" Or _
  94.                    pvi.Name = "17 Radicacion" Or _
  95.                    pvi.Name = "18 Radicacion" Or _
  96.                    pvi.Name = "19 FCI Pendiente" Or _
  97.                    pvi.Name = "20 FCI Anulación" Then
  98.                        pvi.Visible = False
  99.                    End If
  100.                Next
  101.            End With
  102.  
  103.  
  104.  
  105.  
  106.            xlBook.ShowPivotTableFieldList = False
  107.  
  108.            xlSheet.SaveAs("C:\Informe Diario\InforDiario-201011rafafa.xls", _
  109.                                    Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7)
  110.            xlapp.Workbooks.Close()
  111.  
  112.            xlSheet = Nothing
  113.            xlBook = Nothing
  114.  
  115.            If Not xlapp Is Nothing Then
  116.                xlapp.Quit()
  117.                xlapp = Nothing
  118.            End If
  119.  
  120.            'Mato todos los procesos de excel.
  121.            Try
  122.                For Each vl_objProcess As Process In System.Diagnostics.Process.GetProcesses()
  123.                    If vl_objProcess.ProcessName = "EXCEL.EXE" Or _
  124.                        vl_objProcess.ProcessName = "EXCEL" Then
  125.                        vl_objProcess.Kill()
  126.                    End If
  127.                Next
  128.            Catch ex As Exception
  129.  
  130.            End Try
  131.            MessageBox.Show("Listo")
  132.        Catch ex As Exception
  133.            MessageBox.Show(ex.ToString)
  134.        End Try
  135.    End Sub

espero les alla servido


« Última modificación: 30 Diciembre 2010, 18:41 pm por Novlucker » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines