s-news
[Top] [All Lists]

Exporting to Excel spreadsheet with multiple worksheets

To: <s-news@lists.biostat.wustl.edu>
Subject: Exporting to Excel spreadsheet with multiple worksheets
From: "Thompson, Trevor" <tkt2@cdc.gov>
Date: Wed, 13 Apr 2005 14:44:53 -0400
Thread-index: AcVAVDBI9L9xzDBZSuKDwKSmdWOUPQABIttg
Thread-topic: Exporting to Excel spreadsheet with multiple worksheets
Hi,

I'm trying to write out matrices within a loop to different worksheets
in the same Excel spreadsheet.  I was trying to do this with the
openData (using the pageNumber argument to specify the worksheet) and
writeNextDataRows functions.  When I do this, it only writes to one
worksheet and overwrites the data as the loop increments.  I am able to
read from multiple worksheets ok.  See examples below:

### Creates 3 dataframes from 3 worksheets in Excel file

for(i in 1:3) {

   out.file <- openData("test.xls", type="Excel", openType="read",
pageNumber = i)

   if(i==1) test.df1<-readNextDataRows(out.file)
   if(i==2) test.df2<-readNextDataRows(out.file)        
   if(i==3) test.df3<-readNextDataRows(out.file)        

   closeData(out.file)

}


#### Does not write to multiple sheets; overwrites 1st sheet

for(i in 1:3) { 

   out.file <- openData("test2.xls", type="Excel", openType="write",
pageNumber = i) 
   x <- matrix(rnorm(50), ncol=5) 
   writeNextDataRows(out.file, x) 
   closeData(out.file) 
} 

Does the pageNumber argument only work for openType="read"?  Is there
any other way to export to multiple worksheets?  

Thanks for any advice.  I am using Splus 6.1 on MS Windows 2000.

-Trevor

<Prev in Thread] Current Thread [Next in Thread>