Open Source C++ Library for Excel Spreadsheet
Read, Write, Edit and Convert Microsoft® Excel Spreadsheet files via Open Source C++ API.
What is OpenXLSX?
OpenXLSX is an open-source C++ library that gives computer programmers the power to read, write, create and modify Excel files as well as data formatting with lesser dependencies. It is a work in progress and aims to provide all the important features for spreadsheet creation and manipulation.
OpenXLSX provides support for several important features, such as creating, opening, and saving spreadsheet files, reading or writing & modify cell contents, Copying cells and cell ranges, Copying worksheets & many more. OpenXLSX can be built and run on several platforms such as Windows, Linux & Linux.
Getting Started with OpenXLSX
The current stable version is available on CRAN. Please use the following command to install it.
Install OpenXLSX via CRAN
install.packages("openxlsx", dependencies = TRUE)
Development version
install.packages("openxlsx", dependencies = TRUE)
require(devtools)
install_github("ycphs/openxlsx")
C++ API to Create & Modify Spreadsheet Files
The open-source API OpenXLSX enables programmers to generate an excel spreadsheet from scratch. It also provides support for modifying the existing spreadsheet with ease. To modify an existing file you need to open it and insert the data you want to include in the files. You can create a new worksheet and add it to the empty workbook, add data to it, and much more. You can also assign a name to the sheet and can also add content to it. By using the following steps, you can create a Microsoft Excel document in C++
Create Excel Document in C++
- Initialize XLDocument object
- Create document
- Add worksheet in document
- Add text in cell
- Save document
Create Excel Easily - C++
// initialize XLDocument
XLDocument doc;
// create document
doc.create("./Demo01.xlsx");
// add worksheet
auto wks = doc.workbook().worksheet("Sheet1");
// add text
wks.cell(XLCellReference("A1")).value() = " Hello OpenXLSX! ";
// save document
doc.save();
Protect Workbook or Worksheet
Evert organizations work hard to collect their required data and don’t want anyone to play with their data. OpenXLSX provides developers with the functionality to prevent people from editing various parts of a workbook. You can protect it by providing a password to limit who can even open it. It is always useful to protect a certain part of a workbook and let the users make changes to other parts when required.
Protect Excel Workbook from Modifications via C++
wb <- createWorkbook()
addWorksheet(wb, "S1")
writeDataTable(wb, 1, x = iris[1:30, ])
# Formatting cells / columns is allowed , but inserting / deleting columns is protected:
protectWorksheet(wb, "S1",
protect = TRUE,
lockFormattingCells = FALSE, lockFormattingColumns = FALSE,
lockInsertingColumns = TRUE, lockDeletingColumns = TRUE
)
# Remove the protection
protectWorksheet(wb, "S1", protect = FALSE)
## Not run:
saveWorkbook(wb, "pageSetupExample.xlsx", overwrite = TRUE)
Manage Worksheet Column Widths
The Open-source API OpenXLSX enables software programmers to set worksheet column widths to a specific width or "auto" for automatic width sizing. We can use widths = "auto" in the setColWidths function to auto-widen the column based on the data. You can also use merge cells to create a header for the data frame’s column headers. It also provides features for hiding the columns.