How to export a DataFrame to Excel File in R ?
Last Updated :
21 Apr, 2021
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 package is used to export our dataframe to an Excel file.
Getting Started
The writexl is a simple package that contains a function write_xlsx() function which is used to write a dataframe to an Excel (.xlsx) file.
Installation
Packages are installed in R by using the install.packages() function. To install a package pass the name of the package/library to the install.packages() function.
We can install the writexl package by running the command below in the R console.
install.packages("writexl")
Export the dataframe to excel
Example 1: In the first line of the code below, we have used library(“writexl”) function to load the package named -> “writexl”. We have then used write_xlsx() function of the writexl library to export the dataframe to an Excel file. In the below example, our write_xlsx() function takes 2 arguments, the first argument is the dataframe itself which is to be converted to an Excel file, the second argument is the path with “file_name.xlsx” which specifies the location where our Excel file will be saved with specified file_name.
To create a “.xlsx” with (multiple) named sheets, simply set “df” to a named list of data frames.
Syntax: write_xlsx(dataframe_name, “path\\file_name.xlsx”)
Code:
R
library ( "writexl" )
df <- data.frame (name = c ( "This" , "is" , "GFG" ),
roll = c (10,20,30))
df
write_xlsx (df, "MY_PATH\df.xlsx" )
|
Output:

Our dataframe gets exported in the form of an Excel file to the specified location or path.
Example 2:
The only difference in this example from the previous examples is the arguments, in the example code below we have passed 2 extra arguments which help us have more control over the formatting of the Excel file. Those arguments and their usage are:
- col_names: write column names at the top of the Excel file(.xlsx)
- format_headers: make the column names(col_names) in the Excel file(.xlsx) centered and bold
Syntax:
write_xlsx(
df,
"path\\file_name.xlsx"),
col_names = TRUE,
format_headers = TRUE
)
Code:
R
library ( "writexl" )
df <- data.frame (name = c ( "This" , "is" , "GFG" ),
roll = c (10,20,30))
df
write_xlsx (
df, "My_path\\df.xlsx" ,
col_names = TRUE ,
format_headers = TRUE )
|
Output:

Our dataframe gets exported in the form of an excel file to the specified location or path.
Export the dataframe to a CSV
The write.csv() is an inbuilt function in R, we do not require installing any additional library for using this function.
In the code below, our write.csv() function takes 2 arguments, the first argument is the dataframe itself which is to be converted to a CSV file, the second argument is the path with “file_name.csv” which specifies the location where our CSV file will be saved with specified file_name.
Syntax: write.csv(dataFrame_name, “path\\file_name.csv”)
Code:
R
df <- data.frame (name = c ( "This" , "is" , "GFG" ),
roll = c (10,20,30))
df
write.csv (df, "My_Path\\df.csv" )
|
Output:

Similar Reads
How to export dataframe to RDATA file in R?
In this, article we are going to save the information of a data frame in an RDATA file and display the information of the file using R Programming language. To save the information of a data frame in a file and display the information of the file in R language is as follows: Using the save function
3 min read
How to Export DataFrame to CSV in R ?
R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach: Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CSV
1 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 Delete DataFrames in R?
In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
3 min read
Export Dataframes to Multiple Excel Sheets in R
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
5 min read
How To Export a Matrix as a CSV File in MATLAB?
A CSV file - Comma Separated File, as the name suggests, is a file that stores data delimited by a ' , '. In MATLAB, there is a simple way of exporting a matrix to a csv file. The writematrix() function is used to write a matrix into any desired file type. The syntax is writematrix(, "filename.exten
2 min read
How to Export SQL Server Data to a CSV File?
Here we will see, how to export SQL Server Data to CSV file by using the 'Import and Export wizard' of SQL Server Management Studio (SSMS). CSV (Comma-separated values): It is a file that consists of plain text data in which data is separated using comma(,). It is also known as Comma Delimited Files
2 min read
How to add header row to a Pandas Dataframe?
A header necessarily stores the names or headings for each of the columns. It helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. There are two approaches to add header row to a Pandas Datafra
4 min read
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 read excel file in R ?
We may work with structured data from spreadsheets, take advantage of R's capabilities for data analysis and manipulation, and incorporate Excel data into other R processes and packages by reading Excel files in R. The readxl package offers a simple and effective method for reading Excel files into
3 min read