[Solved] Import a .csv file into Base

dBase, Calc, CSV, MS ACCESS, MySQL, PostgrSQL, OTHER
Post Reply
showerdays
Posts: 4
Joined: Mon Jul 12, 2010 12:15 pm

[Solved] Import a .csv file into Base

Post by showerdays »

Good morning everyone and thanks for everything provided in the forum.

Through an open dialog box I get the path of the directory you want opened.

What I need is now to import this file is a. Csv file to a database table using a

macro to do it all. I used the ImporEntireSpreddSheet but I have not gotten to

work properly, if you have any idea how I would really appreciate it.

Thank you.
Last edited by showerdays on Thu Jul 22, 2010 3:13 pm, edited 1 time in total.
Open Office 3.2 Windows XP
RPG
Volunteer
Posts: 2261
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Import a .csv file into base

Post by RPG »

Hello

Have you search on the tutorial part of this forum.

http://user.services.openoffice.org/en/ ... m.php?f=83
and found

http://user.services.openoffice.org/en/ ... 83&t=23260


Romke
LibreOffice 24.8.5.2 on openSUSE Leap 15.6
showerdays
Posts: 4
Joined: Mon Jul 12, 2010 12:15 pm

Re: Import a .csv file into Base

Post by showerdays »

tHANKX FOR ALL,

THIS IS MY FINAL SOLUTION, THE MACRO WORK IN THIS WAY:

1.-you execute the sub AbrirArchivo,

2.-the you pick the .csv file you want to import

3.-you have a table with file dates!

Code: Select all

REM MACRO PARA IMPORTAR EL ARCHIVO .CSV

Sub sCargaCSV(NombreF As String)
   Dim sSQL As String
   Dim oStat As Object
   Dim sTit As String
   Dim sMsg As String

   If Trim(NombreF)="" Then Exit Sub

   With ThisDatabaseDocument.CurrentController
      If Not .IsConnected Then .Connect
      oStat=.ActiveConnection.CreateStatement
   End With

   On Error Goto ErrorImporta

   sSQL="DROP TABLE ""Origen"" IF EXISTS"
   oStat.ExecuteUpdate(sSQL)

   sSQL="CREATE TEXT TABLE ""Origen""(""ID"" INTEGER,""Campo1"" VARCHAR(30),""Campo2"" VARCHAR(30))"
   oStat.ExecuteUpdate(sSQL)
   
   sSQL="SET TABLE ""Origen"" SOURCE """ & NombreF & ";encoding=UTF-8;fs=,;ignore_first=true"""
   oStat.ExecuteUpdate(sSQL)

'   sSQL="DELETE FROM ""Origen"""
'   oStat.ExecuteUpdate(sSQL)


'   sSQL="ALTER TABLE ""Origen"" ALTER COLUMN ""ID"" RESTART WITH 0"
'   oStat.ExecuteUpdate(sSQL)

   rem AQUI ME DABA ERROR PERA AL FINAL TRABAJO CON UNA TABLA
   REM sSQL="INSERT INTO ""Destino""(""Campo1"",""Campo2"") SELECT ""Campo1"",""Campo2"" FROM ""Origen"""
   REM oStat.ExecuteUpdate(sSQL)
   
	REM NO BORRAR QUE ES LA BUENA
   REM sSQL="DROP TABLE ""Origen"" IF EXISTS"
  REM oStat.ExecuteUpdate(sSQL)

   On Error Goto 0
   
   sTit="Importar desde CSV"
   sMsg="Datos importados con éxito."
   MsgBox sMsg,MB_ICONINFORMATION,sTit
   Exit Sub

   ErrorImporta:
   sMsg="Ocurrió un error mientras se importaban los datos."
   sMsg=sMsg & Chr(13) & Chr(13) & "Error " & Err & " en línea " & Erl & ":" & Chr(13) & Error$
   MsgBox sMsg,MB_ICONEXCLAMATION,"ERROR"
   On Error Goto 0
End Sub



rem Macro para abrir archivo

Sub AbrirArchivo1()
Dim oDlgAbrirArchivo as Object
Dim mArchivo() As String
Dim mOpciones()
Dim sRuta As String
Dim oDoc As Object

        'Creamos el servicio necesario
        oDlgAbrirArchivo = CreateUnoService ("com.sun.star.ui.dialogs.FilePicker")
        'Establecemos el titulo del cuadro de dialogo
        oDlgAbrirArchivo.setTitle("Selecciona el archivo a abrir")
        'Con el metodo .Execute() mostramos el cuadro de dialogo
        'Si el usuario presiona Abrir el metodo devuelve 1 que podemos evaluar como Verdadero (True)
        'Si presiona Cancelar devuelve 0
        If oDlgAbrirArchivo.Execute() Then
                'De forma predeterminada, solo se puede seleccionar un archivo
                'pero devuelve una matriz de todos modos con la ruta completa
                'del archivo en formato URL
                mArchivo() = oDlgAbrirArchivo.getFiles()
                'El primer elemento de la matriz es el archivo seleccionado
                sRuta = mArchivo(0)
                'Y lo abrimos
                rem oDoc = StarDesktop.loadComponentFromURL( sRuta, "_blank", 0, mOpciones() )
                
          msgbox sRuta
          call Cascabel(sRuta)            
        Else
                'Si el usuario presiona Cancelar
                MsgBox "Proceso cancelado"
        End If
        
End Sub

rem parsear el nombre de la ruta
sub Cascabel(ruta as String)

rem ruta=ConvertToURL(sRuta)

Dim sAbsoluto As String
Dim i As Integer
Dim c As String
sAbsoluto=ThisDataBaseDocument.Location
msgbox sAbsoluto
For i=Len(sAbsoluto) To 1 Step -1
   c=Mid(sAbsoluto,i,1)
   If c="/" Or c="\" Then
      sAbsoluto=Left(sAbsoluto,i)
      Exit For
   End If
Next
msgbox sAbsoluto
sAbsoluto=ConvertToURL(sAbsoluto)

If Left(ruta,Len(sAbsoluto))<>sAbsoluto Then
   MsgBox "Por favor, eliga un archivo .csv en" & Chr(13) & sAbsoluto & Chr(13) & "o en una subcarpeta."
   Exit Sub
End If
ruta=Mid(ruta,Len(sAbsoluto)+1)
msgbox ruta
call sCargaCSV(ruta)
end sub
Open Office 3.2 Windows XP
Post Reply