saveWorkbook-methods: Saving Microsoft Excel workbooks

saveWorkbook-methodsR Documentation

Saving Microsoft Excel workbooks

Description

Saves a workbook to the corresponding Excel file. This method actually writes the workbook object to disk.

Usage

## S4 method for signature 'workbook,missing'
saveWorkbook(object,file)
## S4 method for signature 'workbook,character'
saveWorkbook(object,file)

Arguments

object

The workbook to save

file

The file to which to save the workbook ("save as"). If not specified (missing), the workbook will be saved to the workbook's underlying file which is the file specified in loadWorkbook (also see the workbook class 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 the file argument may only specify another xls file. Also note that when specifying the file argument the workbook's underlying filename changes to reflect the "save as" behavior.
Paths are expanded using path.expand.

Details

Saves the specified workbook object to disk.

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.

Author(s)

Martin Studer
Mirai Solutions GmbH https://mirai-solutions.ch

See Also

workbook, loadWorkbook

Examples

## Not run: 
# 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")

## End(Not run)

XLConnect documentation built on Sept. 11, 2024, 8:04 p.m.