Skip to content

Instantly share code, notes, and snippets.

@bedantaguru
Last active July 23, 2018 04:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedantaguru/54b1c7669646cad80fbe9c67c30cb44b to your computer and use it in GitHub Desktop.
Save bedantaguru/54b1c7669646cad80fbe9c67c30cb44b to your computer and use it in GitHub Desktop.
Convert XLS to XLSX with the Help of RDCOMClient and Excel
library(RDCOMClient)
convert_xls_to_xlsx<- function(in_folder,out_folder, delete_xls=F){
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))
}
# Main section of the code which is responsible for conversion is give here :-
xls ("Excel.Application")
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment