我有一个带有公司徽标和格式化的默认工作簿,以便快速为我的客户生成工作簿,而无需一直重新格式化所有内容。
我设法为一张纸做。我想在一本工作簿中为我需要的多张工作表做这件事。
我现在这样做,如下
wb = loadWorkbook("O:/R/handle_wb.xlsx") # loads default workbook
sheets = getSheets(wb) # get pre-formatted sheet
# change styles
cs = CellStyle(wb) + ...
# add df of data to excel with the chosen styles
addDataFrame(data, sheets$Sheet1, startRow = 6, startColumn = 1,
colnamesStyle = cs, row.names = F)
然后,我想在同一个wb中生成另一个工作表,但是使用我已经预先格式化的工作表,通过编写像
这样的函数 add.sheet <- funtion(newdata, original.wb, default.wb){
wb = loadWorkbook(default.wb) # loads default workbook
sheets = getSheets(wb) # get pre-formatted sheet
ob = loadWorkbook(original.wb) # loads orginal workbook
# change styles
cs = CellStyle(wb) + ...
# add df of data to excel with the chosen styles
addDataFrame(newdata, sheets$Sheet1, startRow = 6, startColumn = 1, colnamesStyle = cs, row.names = F)
# create a new sheet in the original workbook to receive the newsheet created above
createSheet(ob, "sheet2")
she <- getSheets(ob)
she$sheet2 <- sheets$Sheet1 # designate to sheet2 the created default sheet
saveWorkbook(ob, original.wb)
}
问题是,如何将我在原始工作簿中创建的空白工作表替换为默认工作簿中的空白工作表,即she$sheet2 <- sheets$Sheet1
?