Export Dataframes to Multiple Excel Sheets in R
Last Updated :
15 Apr, 2025
An excel workbook is used to contain tabular information stored within a cell-like structure. Every excel workbook in R contains multiple sheets to contain the data belonging to the same domain information. The excel sheet can contain columns belonging to different data types. The Excel files can be created from data frames using R.
Using xlsx library to Generate multiple xlsx-output in R
The xlsx package in R can be used to perform read, write and manipulation operations with Excel files. It is one of the most prominent packages available in all the varied environments to perform Excel document(xls and xlsx) formatting. It has to be downloaded and installed and downloaded into the working space using the command:
install.packages(“xlsx”)
A data frame can be declared using the data.frame() method, where the columns and rows are declared. Each column is associated with a fixed data type and column name. The data frame stores the data in a tabular-like structure and this can be written to the Excel sheet, with a customized name. The write.xlsx() method in this package is used to write the data frame onto the specified sheet of the workbook. It has the following syntax :
write.xlsx(dataframe , file=filepath , sheetName )
Arguments :
- dataframe – The data frame to be written to the Excel sheet
- filepath – The file path with the file name to be created
- sheetName – The name of the sheet to write the data frame to.
In case, the data frames have to be consecutively written to the next sheets, the append option in the write.xlsx() has to be set to TRUE so that the data entered initially in the earlier sheets is not overwritten.
R
df1 = data.frame(col1 = c(1:5),
col2 = c("Michael","Lincoln","Daniel","Fernandes","Eerie"),
col3 = c(TRUE,FALSE,FALSE,TRUE,TRUE))
df2 = data.frame(col1 = letters[1:3],
col2 = c("Monday","Tuesday","Wednesday"))
df3 = data.frame(col1 = c(1.2,2.3,5.3,7.3),
col2 = c("A","B","C","D"))
Now, let’s create the sheet in a progressive manner.
R
library(xlsx)
write.xlsx(df1,
file="/Users/mallikagupta/Desktop/geeks_multiple.xlsx",
sheetName="sht1")
print("Data Frame1")
print(df1)
Output:
[1] “Data Frame1”
col1 col2 col3
1 1 Michael TRUE
2 2 Lincoln FALSE
3 3 Daniel FALSE
4 4 Fernandes TRUE
5 5 Eerie TRUE

Subsheet 1 added to the main sheet
This first data frame has been added as a subsheet-1 , in the main sheet geek_multiple.xlsx.
R
write.xlsx(df2,
file="/Users/mallikagupta/Desktop/geeks_multiple.xlsx",
sheetName="sht2", append=TRUE)
print("Data Frame2")
print(df2)
Output:
[1] “Data Frame2”
col1 col2
1 a Monday
2 b Tuesday
3 c Wednesday
.png)
Subsheet 2 added to the main sheet
This second data frame has been added as a subsheet-2 , in the main sheet geek_multiple.xlsx.
R
write.xlsx(df3,
file="/Users/mallikagupta/Desktop/geeks_multiple.xlsx",
sheetName="sht3", append=TRUE)
print("Data Frame3")
print(df3)
Output:
[1] “Data Frame3”
col1 col2
1 1.2 A
2 2.3 B
3 5.3 C
4 7.3 D
.png)
Subsheet 3 added to the main sheet
On execution, we have successfully added the third data frame as the sub-sheet number three , in our main excel file geek_multiple.xlsx. So, we’ve filled the geek_multiple.xlsx
file with several sheets, each containing a different data frame.
Using openxlsx library to Generate multiple xlsx-output in R
The openxlsx package in R can be downloaded and installed into the R environment and is used to perform read/write and format transactions conveniently. It can be used to write data frames or Tibbles onto Excel sheets. It can also be used to style the worksheets.
install.packages(“openxlsx”)
The data frames that are created can be simultaneously mapped to the sheets of the Excel workbook, using the list method. An associative mapping of the sheet-name pointing to the data frame value is created. This creates a list of data frames.
list(key = value, .. )
Arguments :
keyvalue – The key-value pairs indicative of the sheet name and the corresponding data frame.
Then, the write.xlsx() method is then invoked in order to write the entire list of data frames to the specified file path.
R
library(openxlsx)
data_frames <- list("Sheet 1" = df1, "Sheet 2" = df2, "Sheet 3" = df3)
write.xlsx(data_frames,
file = "/Users/mallikagupta/Desktop/multiple_outputs.xlsx")
Output:

Subsheet 1 added to the main sheet
.png)
Subsheet 2 added to the main sheet
.png)
Subsheet 3 added to the main sheet
Using writexl library to Generate multiple xlsx-output in R
The writexl package is used to provide the write_xlsx() method which writes the data frame onto the Excel sheet, specified with the extension, xls and xlsx. It can be downloaded and installed into the working environment using the following command :
install.packages(writexl)
The wirte_xlsx() method is used to write a list of data frames onto the specified file path location. The data frames are created on the sheet specified in the file path location.
write_xlsx(list-of-df, file-path)
Arguments :
- listofdf – The data frames to be written to the specified file path
- filepath – The file path to write the xlsx document to
Let’s create a data frame for our main sheet.
R
df1 = data.frame(col1 = c(1:5),
col3 = c(TRUE,FALSE,FALSE,TRUE,TRUE))
print("Data Frame1")
print(df1)
Output:
[1] “Data Frame1”
col1 col3
1 1 TRUE
2 2 FALSE
3 3 FALSE
4 4 TRUE
5 5 TRUE
Now let’s create another data frame so, that we can make the main sheet with at least two sub-sheets in it.
R
df2 = data.frame(col1 = letters[1:3],
col4 = c(10,20,30)
)
print("Data Frame2")
print(df2)
Output:
[1] “Data Frame2”
col1 col4
1 a 10
2 b 20
3 c 30
Now, let’s create the main sheet with subsheet1 containing data from the first data frame and sub-sheet 2 containing data from the second data frame.
R
library(writexl)
data_frames <- list("sht1" = df1, "sht2" = df2)
write_xlsx(data_frames,
"/Users/mallikagupta/Desktop/approach3.xlsx")
Output:
.png)
Subsheet 1 added to the main sheet
.png)
Subsheet 2 added to the main sheet
Similar Reads
How to Export Multiple Dataframes to Different Excel Worksheets in R
In this article, we are going to see how to export multiple dataframe to different Excel Worksheets in R Programming Language. We will be using the xlsx module. The xlsx library provides R functions to read/write/format Excel files and their formats. The xlsx package gives programmatic control of Ex
4 min read
How to Write Pandas DataFrames to Multiple Excel Sheets?
In this article, we will see how to export different DataFrames to different excel sheets using python. Pandas provide a function called xlsxwriter for this purpose. ExcelWriter() is a class that allows you to write DataFrame objects into Microsoft Excel sheets. Text, numbers, strings, and formulas
6 min read
How to export a DataFrame to Excel File in R ?
It is a frequent requirement to save our dataframe for portability after working on it on the auxiliary memory of the computer system using R Programming Language. In this article, we will be using writexl package to export our dataframe to excel(.xlsx). The write_xlsx() function of the writexl pack
3 min read
Import multiple excel sheets into in R
In this article, we are going to see how to import multiple Excel sheets into the R language. Excel provides us with multiple worksheets. For example, in the below Excel workbook StudentData, we have two worksheets - sheet 1 is Student Details and sheet 2 is Subject Details. For importing multiple E
2 min read
Exporting a Pandas DataFrame to an Excel file
Sometimes we need an Excel file for reporting, so as a coder we will see how to export Pandas DataFrame to an Excel file. The to_excel() function in the Pandas library is utilized to export a DataFrame to an Excel sheet with the .xlsx extension. Syntax # saving the exceldataframe_name.to_excel(file_
4 min read
How to Create a Chart from Multiple Sheets in Excel
Creating a chart from multiple sheets in Excel is a powerful way to consolidate data and visualize it in a meaningful way. Whether you're working with different datasets on separate sheets or need to compare data across multiple tabs, knowing how to create a chart from multiple sheets in Excel can s
5 min read
How to Create Multiple Sheets in Excel workbook Using R
In this article, we will discuss how to create multiple sheets in an excel file using the xlsx package. As we all know in general an excel file might contain one or more than one sheet present in it. Manually we can create and insert data into multiple sheets in Excel GUI Application but when it co
2 min read
Combine Multiple Excel Worksheets into Single Dataframe in R
In this article, we will discuss how to combine multiple excel worksheets into a single dataframe in R Programming Language. The below XLSX file "gfg.xlsx" has been used for all the different approaches. Method 1: Using readxl package The inbuilt setwd() method is used to set the working directory i
4 min read
How to Combine Data from Multiple Sheets in one File?
Merging data into a single sheet is a crucial and significant skill to know. For example, you have the monthly sales of a company in the 12 worksheets of excel. Now you want to calculate the net revenue made in the entire year. So, we need to merge all the data in the 12 excel sheets, this task coul
4 min read
How to read multiple Excel files in R
In this article, we will discuss how to merge multiple Excel files in the R programming language. Modules Used:dplyr: The dplyr package in R is a structure of data manipulation that provides a uniform set of verbs, helping to resolve the most frequent data manipulation hurdles.plyr: The âplyrâ packa
2 min read