在多个工作表中添加标题

时间:2015-04-07 05:41:25

标签: r xlsx

我们如何在每个导出的工作表中添加标题(在单个工作簿中)。它是以下答案的延伸 - R: easy way to export multiple data.frame to multiple excel worksheets?

示例(我的输出看起来像这样)

(In sheet1)
Credit card details of Mr.x     
Year    Amount  Paid
2010    $10,000     $10,000 
2011    $20,000     $19,000 

(In sheet2)
Population data for the year 2010       
Gender  %   No.
Male    45  12345
Female  55  13456

(In sheet-3)
M/S ABC Limites EMPLOYEE Details        
Name ID SALARY
P     2 $10,000 
Q     3 $20,000 

1 个答案:

答案 0 :(得分:0)

您必须使用 openxlsx软件包 writeData()功能:

library("openxlsx")
# Creating workbook
wb <- createWorkbook()
# Create sheet
addWorksheet(wb, "sheet1")

# Write title in worksheet
writeData(wb, 1, x = "Main Title", startRow = 1, startCol = 1)
addStyle(wb = wb, sheet = 1, rows = 1, cols = 1, style = main_title)
# Write subtitle of a given dataset
writeData(wb, 1, x = "Subtitle", startRow = 3, startCol = 1)
addStyle(wb = wb, sheet = 1, rows = 3, cols = 1, style = second_title)

# Write dataset
writeDataTable(wb, 1, x = head(mtcars), startRow = 4,
               startCol = 1, tableStyle = "TableStyleMedium2", withFilter = FALSE)

# Save workbook
openxlsx::saveWorkbook(wb, "test.xlsx")