Learn R Programming

⚠️There's a newer version (1.10) 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.

Even though the project is already well progressed and supports most of the features known and appreciated from the predecessor, there may still be open gaps in one or the other place. A quick warning: Until the stable version 1.0 there may will still be some changes to the API.

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 and xlsm (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/readTest.xlsx", package = "openxlsx2")
read_xlsx(path)
#>     Var1 Var2 NA  Var3  Var4       Var5         Var6    Var7
#> 2   TRUE    1 NA     1     a 2015-02-07 3209324 This #DIV/0!
#> 3   TRUE   NA NA #NUM!     b 2015-02-06         <NA>    <NA>
#> 4   TRUE    2 NA  1.34     c 2015-02-05         <NA>   #NUM!
#> 5  FALSE    2 NA  <NA> #NUM!       <NA>         <NA>    <NA>
#> 6  FALSE    3 NA  1.56     e       <NA>         <NA>    <NA>
#> 7  FALSE    1 NA   1.7     f 2015-02-02         <NA>    <NA>
#> 8     NA   NA NA  <NA>  <NA> 2015-02-01         <NA>    <NA>
#> 9  FALSE    2 NA    23     h 2015-01-31         <NA>    <NA>
#> 10 FALSE    3 NA  67.3     i 2015-01-30         <NA>    <NA>
#> 11    NA    1 NA   123  <NA> 2015-01-29         <NA>    <NA>

# or import workbooks
wb <- wb_load(path)
wb
#> A Workbook object.
#>  
#> Worksheets:
#>  Sheets: Sheet1 Sheet2 Sheet 3 Sheet 4 Sheet 5 Sheet 6 1 11 111 1111 11111 111111 
#>  Write order: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

# read a data frame
wb_to_df(wb)
#>     Var1 Var2 NA  Var3  Var4       Var5         Var6    Var7
#> 2   TRUE    1 NA     1     a 2015-02-07 3209324 This #DIV/0!
#> 3   TRUE   NA NA #NUM!     b 2015-02-06         <NA>    <NA>
#> 4   TRUE    2 NA  1.34     c 2015-02-05         <NA>   #NUM!
#> 5  FALSE    2 NA  <NA> #NUM!       <NA>         <NA>    <NA>
#> 6  FALSE    3 NA  1.56     e       <NA>         <NA>    <NA>
#> 7  FALSE    1 NA   1.7     f 2015-02-02         <NA>    <NA>
#> 8     NA   NA NA  <NA>  <NA> 2015-02-01         <NA>    <NA>
#> 9  FALSE    2 NA    23     h 2015-01-31         <NA>    <NA>
#> 10 FALSE    3 NA  67.3     i 2015-01-30         <NA>    <NA>
#> 11    NA    1 NA   123  <NA> 2015-01-29         <NA>    <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-2022). Both released under the MIT license.

Copy Link

Version

Install

install.packages('openxlsx2')

Monthly Downloads

6,249

Version

0.6.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Last Published

April 21st, 2023

Functions in openxlsx2 (0.6.1)

create_cell_style

create_cell_style
dataframe_to_dims

create dimensions from dataframe
create_hyperlink

create Excel hyperlink string
dims_to_dataframe

create dataframe from dimensions
create_numfmt

create number format
create_dxfs_style

Create a custom formatting style
create_sparklines

create sparklines used in add_sparline()
get_cell_refs

Return excel cell coordinates from (x,y) coordinates
create_border

create border
numfmt_is_posix

check if numFmt is posix. internal function
create_fill

create fill
guess_col_type

function to estimate the column type. 0 = character, 1 = numeric, 2 = date.
int2col

Convert integer to Excel column
openxlsx2

xlsx reading, writing and editing.
%>%

Pipe operator
print.pugi_xml

print pugi_xml
read_xml

read xml file
read_xlsx

Read from an Excel file or Workbook object
select_active_sheet

get and set table of sheets and their state as selected and active
sheet_visibility

Get/set worksheet visible state
get_date_origin

Get the date origin an xlsx file is using
style_mgr

style manager
styles_on_sheet

get all styles on a sheet
numfmt_is_date

check if numFmt is date. internal function
named_region

Create / delete a named region
style_is_posix

check if style is posix. internal function
style_is_date

check if style is date. internal function
pugixml

xml_node
read_sheet_names

Get names of worksheets
wbChartSheet

R6 class for a Workbook Chart Sheet
wb_color

Create a new wbColour object
wbSheetData

R6 class for a Workbook Hyperlink
waivers

openxlsx2 waivers
wbComment

R6 class for a Workbook Comments
wbWorksheet

R6 class for a Workbook Worksheet
temp_xlsx

helper function to create temporary directory for testing purpose
wb_add_border

add border for cell region
wbHyperlink

R6 class for a Workbook Hyperlink
wbWorkbook

R6 class for a Workbook
wb_add_conditional_formatting

Add conditional formatting to cells
wb_add_chartsheet

Add a chartsheet to a workbook
wb_add_cell_style

add cell style for cell region
wb_add_chart_xml

add a chart xml to a workbook
wb_add_filter

Add column filters
wb_add_drawing

add drawings to workbook
wb_add_data_validation

Add data validation to cells
wb_add_font

add font for cell region
wb_add_fill

add fill for cell region
wb_add_data_table

Add data to a worksheet as an Excel table
wb_add_sparklines

add sparklines to workbook
wb_add_style

add style to workbook
wb_add_pivot_table

Add pivot table to a worksheet
wb_add_page_break

Add a page break to a worksheet
wb_add_numfmt

add numfmt for cell region
wb_add_mschart

Add mschart object to an existing workbook
wb_add_plot

Insert the current plot into a worksheet
wb_add_form_control

Add form control Checkbox, Radiobuttons or Dropmenu
wb_add_image

Insert an image into a worksheet
wb_add_formula

Add a character vector as an Excel Formula
wb_add_worksheet

Add a worksheet to a workbook
wb_copy_cells

copy cells around
wb_clone_worksheet

Clone a worksheet to a workbook
wb_freeze_pane

Freeze a worksheet pane
wb_get_base_font

Return the workbook default font
wb_creators

Workbook creators
wb_read

Read from an Excel file or Workbook object
wb_protect_worksheet

Protect a worksheet from modifications
wb_remove_col_widths

Remove column widths from a worksheet
wb_remove_filter

Remove a worksheet filter
wb_set_bookview

Set the workbook position, size and filter
wb_set_col_widths

Set worksheet column widths
wb_order

Order of worksheets in xlsx file
wb_protect

Protect a workbook from modifications
wb_get_sheet_name

Get sheet name
wb_get_sheet_names

Get worksheet names for a workbook
wb_remove_worksheet

Remove a worksheet from a workbook
wb_open

little worksheet opener
wb_modify_basefont

Modify the default font
wb_data

provide wb_data object as mschart input
wb_save

Save Workbook to file
wb_grid_lines

Set worksheet gridlines to show or hide.
wb_clone_sheet_style

clone sheets style
workbook_grouping

Group Rows and Columns
wb_add_data

Add data to a worksheet
wb_remove_row_heights

Remove custom row heights from a worksheet
wb_get_tables

List Excel tables in a workbook
wb_get_worksheet

Get a worksheet from a wbWorkbook object
wb_remove_tables

Remove an Excel table in a workbook
wb_set_header_footer

Set document headers and footers
wb_set_sheet_names

Set worksheet names for a workbook
wb_load

Load an existing .xlsx file
xml_rm_child

remove xml child to node
wb_set_row_heights

Set worksheet row heights
wb_set_last_modified_by

Add another author to the meta data of the file.
xl_open

Open a Microsoft Excel file (xls/xlsx) or an openxlsx2 wbWorkbook
wb_workbook

Create a new Workbook object
xml_add_child

append xml child to node
wb_to_df

Create Dataframe from Workbook
write_xlsx

write data to an xlsx file
write_formula

Write a character vector as an Excel Formula
ws_cell_merge

Worksheet cell merging
ws_page_setup

Set page margins, orientation and print scaling
xml_node_create

create xml_node from R objects
write_file

write xml file
xml_attr_mod

adds or updates attribute(s) in existing xml node
write_datatable

Write to a worksheet as an Excel table
wb_hyperlink

Create a new hyperlink object
convert_date

Convert from excel date number to R Date type
cell_style

get and set cell style
cleanup

clean sheet (remove all values)
convert_datetime

Convert from excel time number to R POSIXct type.
col2int

Convert Excel column to integer
create_comment

Create, write and remove comments
convertToExcelDate

convert back to ExcelDate
as_xml

loads character string to pugixml and returns an externalptr
clean_worksheet_name

Clean worksheet name
create_font

create font format