WriteXLS(x, ExcelFileName = "R.xls", SheetNames = NULL, perl = "perl",
verbose = FALSE, envir = parent.frame())
NULL
(the default), the
names of the dataframes will be used instead. Worksheet names may
be up to 31 characters in length and must be unique. Ifx
. This defaults to the environment in which
WriteXLS
was called.The actual creation of the Excel file is performed by a Perl script called WriteXLS.pl, which is included with this package.
Note that the named Excel file, if it already exists, will be overwritten and no warning is given. In addition, if the file exists and is open by another application (eg. Excel, OO.org, etc.) you will likely get an error message regarding the inability to open the file and/or that the file is already in use by another application or user. Errors can also occur if the file has been marked as read-only or if your access rights do not allow you to overwrite the file or write to the folder you have indicated in the path to the file.
There is an intermediate step, where the R data frames are first written
to CSV files using write.table
before being written
to the Excel file by the Perl script. tempdir
is used to determine the
current R session temporary directory and a new sub-directory called "WriteXLS"
will be created there. The CSV files will be written to that directory and
both the files and the directory will be deleted prior to the function
terminating normally using on.exit
. It is possible that these
will remain in place if this function terminates abnormally or is aborted
prior to completion.
As write.table
is used to write the data frames to CSV files, the
data types supported by write.table
will be exported to their
character representation correctly. For other data types, it is recommended
that you first coerce them to character columns formatted as you require
and then use WriteXLS
to create the Excel file.
All of the CSV files will be created prior to the creation of the Excel file as the Perl script will loop over them as part of the process. Thus, sufficient free disk space must be available for these files and the Excel file at the same time.
If SheetNames
is specified, a text file called "SheetNames.txt"
will be created in the same temporary directory as the CSV files. This
file will contain the sheet names, one per line and will be used by
the Perl script to name the worksheets in the Excel file.
Each worksheet will be named using either the names in SheetNames
or
the names of the data frames (up to the first 31 characters, which is an Excel
limitation). If any the data frames specified in x
are longer
than 31 characters, they will be truncated to 31
characters. SheetNames
if specified, will be checked to make
sure that all of the entries are less than or equal to 31
characters. If not, an error message will be displayed.
Note that the worksheets must have unique names. Thus, if
SheetNames
is NULL
, the data frame names will be checked
to be sure that they are unique up through the first 31 characters. If
SheetNames
is specified, the entries will be checked to be sure
that they are unique. If not, an error message will be displayed.
Note that the following characters are not allowed for Excel worksheet names: []:*?/ The data frame column names will be exported "as is" and will be the first row in the corresponding worksheet.
The data frame row names will NOT be exported.
UTF-8 encoded content in the data frame should be properly exported using
the Perl Encode
module by default.
Excel 2003 Specifications and Limitations
For Perl Unicode Issues
write.table
and testPerl
# Only run the examples if Perl and all modules are present
if (testPerl(verbose = FALSE))
{
# Examples using built-in data frames
WriteXLS("iris", "iris.xls")
WriteXLS(c("iris", "infert", "esoph"), "Example.xls")
# Clean up and delete XLS files
unlink("iris.xls")
unlink("Example.xls")
}
Run the code above in your browser using DataLab