Adding a dataframe to a sheet without xlsx package

514 Views Asked by At

I'm currently using xlsx package to create and modify Excel files but I need to get rid of Java dependency. I've tried swapping the xlsx package for the openxlsx package but I can't find any simple way to replace addDataFrame function. My code looks like this:

wb = createWorkbook()
sheet = createSheet(wb, "Sheet Name")
addDataFrame(data.frame(MyFile, check.names=FALSE), sheet=sheet, startColumn=1, row.names=FALSE)
saveWorkbook(wb, "MyWB.xlsx")

Is there any function in openxlsx which I can use to add a dataframe to the sheet? Or in any other library which doesn't depend on Java?

1

There are 1 best solutions below

0
On

We use the writeData to add the data frame to a worksheet. For example

# Create a workbook
wb <- openxlsx::createWorkbook()

# Create a worksheet
openxlsx::addWorksheet(wb, "iris_sheet")
# Write data frame to the worksheet
openxlsx::writeData(wb, "iris_sheet", iris)

# Create another worksheet
openxlsx::addWorksheet(wb, "mtcars_sheet")
# Write data frame to this worksheet
openxlsx::writeData(wb, "mtcars_sheet", mtcars)

# Save the whole workbook
openxlsx::saveWorkbook(wb, "./file.xlsx")