1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| install.packages("RPostgreSQL")
install.packages("gdata")
install.packages("DBI")
library(RPostgreSQL)
library(gdata)
library(DBI)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="postgres", user="postgres", password = "postgres", dbname="db_name")
setwd('/home/donnees/')
Integre_Fichier_excel<-function(schema_destination,fichierexcel,nom_table_a_creer,sheetdata,skipdata,sheetcomment,skipcomment)
{
## fonctions importe les donnees excel dans Rdata
importxls<-function(xls,sheet,skip,dec)
{
mycsv<-xls2csv(xls,sheet=sheet,verbose=TRUE, dec = ".")
read.csv(summary(mycsv)$description,quote = "\"",dec =dec,skip=skipdata)
}
RdataCvs<-importxls(fichierexcel,sheetdata,skip=skipdata,dec=".")
#on mets les champs en minuscule pour virer les quotes des fields
names(RdataCvs)<-tolower(names(RdataCvs))
# on force le schema de destination
V1=paste('set search_path to ',schema_destination,';');
dbGetQuery(con,V1)
# si table existe on supprime
V1=paste('drop table if exists ',schema_destination,'.',nom_table_a_creer,';',sep="");
dbGetQuery(con, V1)
#ecriture de la table
V1=paste(nom_table_a_creer,sep="");
dbWriteTable(con,V1,RdataCvs,row.names=FALSE)
# on remet le schema d'origine
V1=paste('set search_path to ',schema_origine,';');
dbGetQuery(con,V1)
#on force les droits
V1=paste('ALTER TABLE ',schema_destination,'.',nom_table_a_creer,' OWNER TO droit_autre ;',sep="");
dbGetQuery(con, V1)
}
# Importe un fichier excel dans postgres
# parametre entrée
# schema_destination : nom du schema de destination
# fichierexcel : Nom du ficier Excel
# nom_table_a_creer : Nom de la table a créer dans le schema de destination
# sheetdata : Numero de la feuille qui contient les données
# skipdata : Saut de ligne des données en debut de fichier excel
# sheetcomment : Numero de la feuille qui contient les commentaires
# skipcomment : Saut de ligne des commentaires en debut de fichier excel |
Partager