我无法通过R在Excel中创建新标签页

时间:2019-04-30 09:21:00

标签: r openxlsx

我想用“条款”和“ Commloans”创建两个选项卡。 但是,当我使用此代码时,“条款”将覆盖“ Commloans”,而我没有这两个选项卡:/

library("openxlsx")
write.xlsx(Comms, file = "PnL.xlsx", sheetName="Commloans", append=TRUE)
write.xlsx(Terms, file = "PnL.xlsx", sheetName="Terms", append=TRUE)

2 个答案:

答案 0 :(得分:0)

没有reprex很难回答,但是我想如果您从第一个调用中删除append = TRUE而仅将其留在第二个调用上,则应该使用xlsx

所以...

xlsx::write.xlsx(Comms, file = "PnL.xlsx", sheetName="Commloans")
xlsx::write.xlsx(Terms, file = "PnL.xlsx", sheetName="Terms", append=TRUE)

答案 1 :(得分:0)

您需要先创建一些工作表,然后使用write.xlsx 。看到这个答案; R: easy way to export multiple data.frame to multiple excel worksheets?

我建议使用openxlsx软件包

library(openxlsx)

# Create a blank workbook
OUT <- createWorkbook()

# Add some sheets to the workbook
addWorksheet(OUT, "Sheet 1 Name")
addWorksheet(OUT, "Sheet 2 Name")

# Write the data to the sheets
writeData(OUT, sheet = "Sheet 1 Name", x = dataframe1)
writeData(OUT, sheet = "Sheet 2 Name", x = dataframe2)

# Export the file
saveWorkbook(OUT, "My output file.xlsx")