
Saving Microsoft Excel workbooks
saveWorkbook-methods.RdSaves a workbook to the corresponding Excel file. This method actually writes the workbook object to disk.
Usage
# S4 method for class 'workbook,missing'
saveWorkbook(object,file)
# S4 method for class 'workbook,character'
saveWorkbook(object,file)Arguments
- object
The
workbookto save- file
The file to which to save the
workbook("save as"). If not specified (missing), the workbook will be saved to theworkbook's underlying file which is the file specified inloadWorkbook(also see theworkbookclass for more information). Note that due to currently missing functionality in Apache POI, workbooks can only be saved in the same file format - i.e. if the workbooks underlying file format is xls, then thefileargument may only specify another xls file. Also note that when specifying thefileargument theworkbook's underlying filename changes to reflect the "save as" behavior.
Paths are expanded usingpath.expand.
Details
Saves the specified workbook object to disk.
Author
Martin Studer
Mirai Solutions GmbH https://mirai-solutions.ch
Note
As already mentioned in the documentation of the
workbook class, a workbook's
underlying Excel file is not saved (or being created in case the file
did not exist and create = TRUE has been specified) unless the
saveWorkbook method has been called on the object. This provides
more flexibility to the user to decide when changes are saved and also
provides better performance in that several changes can be written in
one go (normally at the end, rather than after every operation causing
the file to be rewritten again completely each time). This is due to the
fact that workbooks are manipulated in-memory and are only written to
disk with specifically calling saveWorkbook.
Further note that calling saveWorkbook more than once leads to an
exception. This is due to a current issue in the underlying POI libraries.
However, with XLConnect there should be no need to call saveWorkbook
more than once so virtually this is no issue.
Examples
if (FALSE) { # \dontrun{
# Create a new workbook 'saveMe.xlsx'
# (assuming the file to not exist already)
wb <- loadWorkbook("saveMe.xlsx", create = TRUE)
# Create a worksheet called 'mtcars'
createSheet(wb, name = "mtcars")
# Write built-in dataset 'mtcars' to sheet 'mtcars' created above
writeWorksheet(wb, mtcars, sheet = "mtcars")
# Save workbook - this actually writes the file 'saveMe.xlsx' to disk
saveWorkbook(wb)
# clean up
file.remove("saveMe.xlsx")
} # }