0

I want to convert 100 excel document lying in "C:/Users/hp/Desktop/excel" to .txt format..I am triyng to use lapply so that all of them are converted to txt at one go.. Please help me .. if I have one file I am converting the .xlsx document to txt in the following way library(xlsx)

sink("output.txt")

data=read.xlsx("C:/Users/hp/Desktop/excel/test2.xlsx",1)

data

sink()

I am writing following code to do it recursively

dest <- "C:/Users/hp/Desktop/excel"

dir(dest)

myfiles <- list.files(path = dest, pattern = "xlsx",  full.names = TRUE)

lapply(myfiles,function(x)  system(sink(x) data=read.xlsx(myfiles,1) data 
sink()),fullnames=TRUE) 

After having done for .xlsx it has to be done for .docx also

r2evans
  • 141,215
  • 6
  • 77
  • 149
mjaay
  • 1
  • Your code in the `lapply` is wrong syntactically, cannot work (probably a copy/paste problem). Is there a reason you aren't using `read.xlsx(...)` followed by `write.csv(...)` or one of the text file variants? – r2evans Apr 09 '17 at 20:00
  • 1
    Perhaps it would help if you identify exactly how you expect your text files to be formatted. For example, comma-delimited, tab-delimited, or aesthetically-oriented R-console-output (not good for later programmatic consumption). – r2evans Apr 09 '17 at 20:15
  • r2evans..This is what came to my mind . I am beginner programmer Actually I was trying to make a lapply function for this problem. – mjaay Apr 10 '17 at 05:41
  • yes we can do it as follows . dta=read.xlsx("C:/Users/Desktop/excel_p/abc.xlsx",1) write.table(dta,"abc.txt") .But I am not getting it how to recursively do it for the folder using laaply – mjaay Apr 10 '17 at 07:08
  • Perhaps this is useful: http://stackoverflow.com/a/5759013/3358272 – r2evans Apr 10 '17 at 12:56

1 Answers1

0

It is possible to export an Excel file to txt with the following code :

library(RDCOMClient)

xlApp <- COMCreate("Excel.Application")
xlApp[["Visible"]] <- TRUE
xlWbk <- xlApp$Workbooks()$Open("D:\\Excel_File.xlsx")
xlWbk$SaveAs(FileName = "D:\\Excel_File.txt", FileFormat = 42) # save to txt, see https://learn.microsoft.com/fr-fr/office/vba/api/excel.xlfileformatf
Emmanuel Hamel
  • 1,769
  • 7
  • 19