Last active
July 23, 2018 04:05
-
-
Save bedantaguru/54b1c7669646cad80fbe9c67c30cb44b to your computer and use it in GitHub Desktop.
Convert XLS to XLSX with the Help of RDCOMClient and Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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