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




# loads the package
library("writexl"
  
# sample dataframe
df <- data.frame(name = c("This", "is", "GFG"),
                 roll = c(10,20,30))
df
  
# saves the dataframe at the specified
# path
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




# loads the package
library("writexl"
  
# sample dataframe
df <- data.frame(name = c("This", "is", "GFG"),
                 roll = c(10,20,30))
df
  
# saves the dataframe at the specified path
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.

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 package is used to export our dataframe to an Excel file.

Similar Reads

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....

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....

Export the dataframe to a CSV

...

Contact Us