# Loop exporting to excel file- writeWorksheetToFile

#### jfca283

##### Member
Hi,
I need to read multiple dta files and exporting the content of a variable
called "name". My idea is to aggregate to the right each "name" from each
dta file. I made a simple loop and I know It works reading the dta files.
However, It can't say the same exporting the data.

Here is what I wrote:

Code:
library(XLConnect)
options(java.parameters = "-Xmx4096m")

setwd("C://Users//Jce//a")
file = list.files(pattern="\\.dta$") myFiles <- list.files(pattern = "\\.dta$")
ttt=length(file)

for(i in myFiles){
for (j in 1:length(file)) {
rm(plop)

plop=myDf\$name
writeWorksheetToFile("datos.xlsx",data=plop, sheet="variables",startRow=1, startCol=j)

}
}
Every time I run the code I received a message with "Error: POIXMLException
(Java): java.lang.reflect.InvocationTargetException".
And when I opened the excel file, It only contained in the first column
infomatio (plop) of the first dta file.
I have no idea what else to do. The error seems related to the intent to
export the data to excel than reading the files.
Thanks for your time and interest.

#### jfca283

##### Member
I've made some test I found out that the package XLConnect isn't reliable when you try to make some loops.
So, I just decided to append every database and after the last file is appended, I've exported using the XLConnect package.
If you ask me how do I know the package XLConnect fails, It is because I can perform partially the code. Using around the 20th file It stops and reports the "Error: POIXMLException
(Java): java.lang.reflect.InvocationTargetException" message.