Do PostgreSQL para o Excel

by Rafael Amorim 9. junho 2008 08:56

 

Essa dica é pra quem precisa importar dados do postgres (ou qualquer outro SGBD) para o excel.

O comando é via macro e eu só usei no Excel 2003.

Sub Executa_SQL_PG(rSql As String, rPlaSaida As String, rCelSaida As String, rIP As String, rPorta As String, rBanco As String, rUsuario As String, rSenha As String, rSchema As String)

    'Esta função conecta no banco de dados, executa o sql e devolve na celula indicada
    ' rSql      => Comando a ser executado
    ' rPlaSaida => Nome da planilha onde os dados vão retornar. Se em branco, retorna na atual
    ' rCelSaida => Endereço de Célula onde os dados vão sair. Se em branco, retorna na A5
    ' rIP       => IP do servidor
    ' rPorta    => Porta onde conectar
    ' rBanco    => Nome do Banco de Dados
    ' rUsuario  => Nome do Usuario
    ' rSenha    => Senha do Usuário
    ' rSchema   => Schema a considerar
      
    
    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stSQL As String
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim rnStart As Range
    Dim stADO As String

    ' valida planilha de saida
    If rPlaSaida <> "" Then
        Sheets(rPlaSaida).Select
    End If

    ' valida celula de saida
    If rCelSaida = "" Then
        rCelSaida = "A5"
    End If

    stADO = "Driver={PostgreSQL UNICODE};Server=" & rIP & ";Port=" & rPorta & ";Database=" & rBanco & ";Uid=" & rUsuario & ";Pwd=" & rSenha & ";"
      
    Set wbBook = ActiveWorkbook
    Set wsSheet = wbBook.Worksheets(rPlaSaida)
    
    With wsSheet
        Set rnStart = .Range(rCelSaida)
    End With
    
    stSQL = rSql
   
    Set cnt = New ADODB.Connection
    
    With cnt
        .CursorLocation = adUseClient
        .Open stADO
        .CommandTimeout = 5000000
        Set rst = .Execute(stSQL)
    End With
    
     'Here we add the Recordset to the sheet from A1
    rnStart.CopyFromRecordset rst
    
    'Cleaning up.
    rst.Close
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing

End Sub

 

Dá pra fazer gravando uma macro usando a importação de dados externos do excel. Mas ai depende de ter uma odbc na maquina e o resultado nao sai da tabela (até sai, mas da muito trabalho), o que aumenta o tamanho dela.

 

É isso. Abraços!

Tags:

Comentários (13) -

payday loans
10/8/2009 11:48:52 #

kagomesesshomaru@aol.com

cash loans
12/8/2009 01:48:25 #

A SUPPORTED BY THE DEVELOPER TOOLS? It was interesting. You seem very knowledgeable in ypour field.

13/8/2009 19:32:57 #

I like how you write.Are you interesting in a part time writer job?

Seiko Mens Sportura Formula
15/8/2009 17:15:12 #

Do you accept guest posts? I would love to write couple articles here.

Square Metal Watch Of
17/8/2009 15:40:46 #

Do you earn decent money from this blog or are you doing it just for fun?

Baume Mercier Classima
26/8/2009 05:18:16 #

Tried to autotranslate you site not understand the writing any hope deutsch version?

bad credit loans
28/9/2009 01:06:04 #

I usually don�t post in Blogs but your blog forced me to, amazing work.. beautiful �

payday loans
18/12/2009 14:26:19 #

I usually don’t post in Blogs but your blog forced me to, amazing work.. beautiful …

fast payday loans
19/12/2009 01:24:14 #

In searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up.You are a very smart person!

fast payday loans
5/1/2010 13:54:27 #

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It's always nice when you can not only be informed, but also entertained! I'm sure you had fun writing this article.

easy payday loans
5/1/2010 14:14:38 #

There are certainly a lot of details like that to take into consideration.

cash loans
10/2/2010 00:56:52 #

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

teeth whitening products
17/2/2010 13:57:28 #

my God, i thought you were going to chip in with some decisive insght at the end there, not leave it with "we leave it to you to decide".

Os comentários estão fechados

Month List