-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxls_to_xlsx.R
48 lines (34 loc) · 1.17 KB
/
xls_to_xlsx.R
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
if (!('RDCOMClient' %in% installed.packages()[, "Package"])){
install.packages('RDCOMClient', repos = 'http://www.omegahat.net/R/')
}
library(RDCOMClient)
convert_xls_to_xlsx<- function(in_folder,out_folder, delete_xls=F){
if(missing(in_folder)){
in_folder<-getwd()
}
if(missing(out_folder)){
out_folder<- in_folder
}
all_xls<- list.files(in_folder, pattern = ".xls$")
if(length(all_xls)>0){
all_xls_out<- gsub(".xls$",".xlsx", all_xls)
try({
xls <- COMCreate("Excel.Application")
lapply(1:length(all_xls), function(i){
cat(i,"\n")
wb = xls$Workbooks()$Open( normalizePath(paste(in_folder, all_xls[i], sep="\\")) )
wb$SaveAs( suppressWarnings( normalizePath(paste(out_folder, all_xls_out[i], sep="\\"))) , 51)
wb$Close()
})
xls$Quit()
}, silent = T)
if(delete_xls){
all_xlsx_now<- list.files(in_folder, pattern = ".xlsx$")
test<- setdiff(gsub(".xls$","", all_xls), gsub(".xlsx$","", all_xlsx_now))
if(length(test)==0){
try(unlink(paste(in_folder, all_xls, sep="\\")),silent = T)
}
}
}
return(invisible(0))
}