Learn R Programming

⚠️There's a newer version (1.9) of this package.Take me there.

openxlsx2

This R package is a modern reinterpretation of the widely used popular openxlsx package. Similar to its predecessor, it simplifies the creation of xlsx files by providing a clean interface for writing, designing and editing worksheets. Based on a powerful XML library and focusing on modern programming flows in pipes or chains, openxlsx2 allows to break many new ground.

Installation

You can install the stable version of openxlsx2 with:

install.packages('openxlsx2')

You can install the development version of openxlsx2 from GitHub with:

# install.packages("remotes")
remotes::install_github("JanMarvin/openxlsx2")

Or from r-universe with:

# Enable repository from janmarvin
options(repos = c(
  janmarvin = 'https://janmarvin.r-universe.dev',
  CRAN = 'https://cloud.r-project.org'))
# Download and install openxlsx2 in R
install.packages('openxlsx2')

Introduction

openxlsx2 aims to be the swiss knife for working with the openxml spreadsheet formats xlsx, xlsm and (limited) xlsb (other formats of other spreadsheet software are not supported). We offer two different variants how to work with openxlsx2.

  • The first one is to simply work with R objects. It is possible to read (read_xlsx()) and write (write_xlsx()) data from and to files. We offer a number of options in the commands to support various features of the openxml format, including reading and writing named ranges and tables. Furthermore, there are several ways to read certain information of an openxml spreadsheet without having opened it in a spreadsheet software before, e.g. to get the contained sheet names or tables.
  • As a second variant openxlsx2 offers the work with so called wbWorkbook objects. Here an openxml file is read into a corresponding wbWorkbook object (wb_load()) or a new one is created (wb_workbook()). Afterwards the object can be further modified using various functions. For example, worksheets can be added or removed, the layout of cells or entire worksheets can be changed, and cells can be modified (overwritten or rewritten). Afterwards the wbWorkbook objects can be written as openxml files and processed by suitable spreadsheet software.

Many examples how to work with openxlsx2 are in our manual pages and in our vignettes. You can find them under:

vignette(package = "openxlsx2")

Example

This is a basic example which shows you how to solve a common problem:

library(openxlsx2)
# read xlsx or xlsm files
path <- system.file("extdata/openxlsx2_example.xlsx", package = "openxlsx2")
read_xlsx(path)
#>     Var1 Var2 NA  Var3  Var4       Var5         Var6    Var7     Var8
#> 3   TRUE    1 NA     1     a 2023-05-29 3209324 This #DIV/0! 01:27:15
#> 4   TRUE   NA NA #NUM!     b 2023-05-23         <NA>       0 14:02:57
#> 5   TRUE    2 NA  1.34     c 2023-02-01         <NA> #VALUE! 23:01:02
#> 6  FALSE    2 NA  <NA> #NUM!       <NA>         <NA>       2 17:24:53
#> 7  FALSE    3 NA  1.56     e       <NA>         <NA>    <NA>     <NA>
#> 8  FALSE    1 NA   1.7     f 2023-03-02         <NA>     2.7 08:45:58
#> 9     NA   NA NA  <NA>  <NA>       <NA>         <NA>    <NA>     <NA>
#> 10 FALSE    2 NA    23     h 2023-12-24         <NA>      25     <NA>
#> 11 FALSE    3 NA  67.3     i 2023-12-25         <NA>       3     <NA>
#> 12    NA    1 NA   123  <NA> 2023-07-31         <NA>     122     <NA>

# or import workbooks
wb <- wb_load(path)
wb
#> A Workbook object.
#>  
#> Worksheets:
#>  Sheets: Sheet1, Sheet2 
#>  Write order: 1, 2

# read a data frame
wb_to_df(wb)
#>     Var1 Var2 NA  Var3  Var4       Var5         Var6    Var7     Var8
#> 3   TRUE    1 NA     1     a 2023-05-29 3209324 This #DIV/0! 01:27:15
#> 4   TRUE   NA NA #NUM!     b 2023-05-23         <NA>       0 14:02:57
#> 5   TRUE    2 NA  1.34     c 2023-02-01         <NA> #VALUE! 23:01:02
#> 6  FALSE    2 NA  <NA> #NUM!       <NA>         <NA>       2 17:24:53
#> 7  FALSE    3 NA  1.56     e       <NA>         <NA>    <NA>     <NA>
#> 8  FALSE    1 NA   1.7     f 2023-03-02         <NA>     2.7 08:45:58
#> 9     NA   NA NA  <NA>  <NA>       <NA>         <NA>    <NA>     <NA>
#> 10 FALSE    2 NA    23     h 2023-12-24         <NA>      25     <NA>
#> 11 FALSE    3 NA  67.3     i 2023-12-25         <NA>       3     <NA>
#> 12    NA    1 NA   123  <NA> 2023-07-31         <NA>     122     <NA>

# and save
temp <- temp_xlsx()
if (interactive()) wb_save(wb, temp)

## or create one yourself
wb <- wb_workbook()
# add a worksheet
wb$add_worksheet("sheet")
# add some data
wb$add_data("sheet", cars)
# open it in your default spreadsheet software
if (interactive()) wb$open()

Authors and contributions

For a full list of all authors that have made this package possible and for whom we are greatful, please see:

system.file("AUTHORS", package = "openxlsx2")

If you feel like you should be included on this list, please let us know. If you have something to contribute, you are welcome. If something is not working as expected, open issues or if you have solved an issue, open a pull request. Please be respectful and be aware that we are volunteers doing this for fun in our unpaid free time. We will work on problems when we have time or need.

License

This package is licensed under the MIT license and is based on openxlsx (by Alexander Walker and Philipp Schauberger; COPYRIGHT 2014-2022) and pugixml (by Arseny Kapoulkine; COPYRIGHT 2006-2023). Both released under the MIT license.

Copy Link

Version

Install

install.packages('openxlsx2')

Monthly Downloads

4,084

Version

1.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Last Published

February 15th, 2024

Functions in openxlsx2 (1.4)

col_widths-wb

Modify column widths of a worksheet
active_sheet-wb

Modify the state of active and selected sheets in a workbook
create_dxfs_style

Create a custom formatting style
as_xml

loads character string to pugixml and returns an externalptr
create_font

Create font format
create_fill

Create fill pattern
comment_internal

Internal comment functions
create_hyperlink

Create Excel hyperlink string
dataframe_to_dims

Create dimensions from dataframe
convertToExcelDate

Convert to Excel data
delete_data

Delete data
filter-wb

Add/remove column filters in a worksheet
fmt_txt

format strings independent of the cell style.
print.pugi_xml

print pugi_xml
properties-wb

Modify workbook properties
grouping-wb

Group rows and columns in a worksheet
create_colors_xml

Create custom color xml schemes
create_comment

Create a comment
create_sparklines

Create sparklines object
create_numfmt

Create number format
read_xml

read xml file
pugixml

xml_node
create_border

Helper to create a border
temp_xlsx

helper function to create temporary directory for testing purpose
waivers

openxlsx2 waivers
wb_add_fill

Modify the background fill color in a cell region
create_cell_style

Helper to create a cell style
wb_add_font

Modify font in a cell region
wb_add_numfmt

Modify number formatting in a cell region
int2col

Convert integer to Excel column
dims_to_dataframe

Create dataframe from dimensions
dims_helper

Helper functions to work with dims
openxlsx2_options

Options consulted by openxlsx2
openxlsx2-package

xlsx reading, writing and editing.
create_tablestyle

Create custom (pivot) table styles
person-wb

Helper for adding threaded comments
%>%

Pipe operator
creators-wb

Modify creators of a workbook
named_region-wb

Modify named regions in a worksheet
openxlsx2-deprecated

Deprecated functions in package openxlsx2
wb_add_drawing

Add drawings to a worksheet
wb_add_dxfs_style

Set a dxfs styling for the workbook
wb_add_form_control

Add a checkbox, radio button or drop menu to a cell in a worksheet
sheet_names-wb

Get / Set worksheet names for a workbook
wb_add_formula

Add a formula to a cell range in a worksheet
row_heights-wb

Modify row heights of a worksheet
wb_add_page_break

Add a page break to a worksheet
wb_add_style

Set the default style in a workbook
wbWorkbook

Workbook class
wb_update_table

Update a data table position in a worksheet
wb_add_worksheet

Add a worksheet to a workbook
wb_base_colors

Set the default colors in a workbook
wb_protect_worksheet

Protect a worksheet from modifications
wb_protect

Protect a workbook from modifications
wb_to_df

Create a data frame from a Workbook
xml_node_create

create xml_node from R objects
wb_clone_worksheet

Create copies of a worksheet within a workbook
wb_clone_sheet_style

Apply styling from a sheet to another within a workbook
wb_add_border

Modify borders in a cell region of a worksheet
wb_add_conditional_formatting

Add conditional formatting to cells in a worksheet
wb_copy_cells

Copy cells around within a worksheet
wb_add_data

Add data to a worksheet
wb_add_cell_style

Modify the style in a cell region
wb_add_ignore_error

Ignore error types on worksheet
wb_add_image

Insert an image into a worksheet
wb_add_thread

Add threaded comments to a cell in a worksheet
wb_add_chart_xml

Add a chart XML to a worksheet
xml_rm_child

remove xml child to node
wb_cell_style

Apply styling to a cell region
wb_add_data_validation

Add data validation to cells in a worksheet
styles_on_sheet

Get all styles on a sheet
sheet_visibility-wb

Get/set worksheet visible state in a workbook
wb_add_data_table

Add a data table to a worksheet
wb_merge_cells

Merge cells within a worksheet
wb_open

Preview a workbook in a spreadsheet software
wb_save

Save a workbook to file
wb_order

Order worksheets in a workbook
wb_data

Add the wb_data attribute to a data frame in a worksheet
wb_page_setup

Set page margins, orientation and print scaling of a worksheet
wb_set_bookview

Set the workbook position, size and filter
write_formula

Write a character vector as an Excel Formula
write_datatable

Write to a worksheet as an Excel table
wb_clean_sheet

Remove all values in a worksheet
wb_workbook

Create a new Workbook object
wb_add_chartsheet

Add a chartsheet to a workbook
write_data

Write an object to a worksheet
wb_add_mschart

Add mschart object to a worksheet
wb_add_named_style

Apply styling to a cell region with a named style
wb_color

Helper to create a color
wb_comment

Helper to create a comment object
wb_add_comment

Add comment to worksheet
wb_add_plot

Insert the current plot into a worksheet
wb_add_pivot_table

Add a pivot table to a worksheet
wb_add_slicer

Add a slicer to a pivot table
wb_get_tables

List Excel tables in a worksheet
wb_add_sparklines

Add sparklines to a worksheet
wb_load

Load an existing .xlsx, .xlsm or .xlsb file
wb_dims

Helper to specify the dims argument
wb_freeze_pane

Freeze pane of a worksheet
wb_remove_tables

Remove a data table from a worksheet
wb_set_sheetview

Modify the default view of a worksheet
wb_set_last_modified_by

Modify author in the metadata of a workbook
wb_remove_worksheet

Remove a worksheet from a workbook
wb_set_header_footer

Set headers and footers of a worksheet
wb_set_grid_lines

Modify grid lines visibility in a worksheet
write_xlsx

Write data to an xlsx file
xl_open

Open an xlsx file or a wbWorkbook object
xml_add_child

append xml child to node
xml_attr_mod

adds or updates attribute(s) in existing xml node
base_font-wb

Set the default font in a workbook
convert_date

Convert from Excel date, datetime or hms number to R Date type
clean_worksheet_name

Clean worksheet name
col2int

Convert Excel column to integer
convert_to_excel_date

convert back to an Excel Date