0
votes

How do we add title in each exported sheet(in a single workbook). It is an extension of the below answer - R: easy way to export multiple data.frame to multiple excel worksheets?

Example (My output looks like this)

(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
It appears to me that your question was answer in that link. Show some code and describe what problems you see.IRTFM
(Maybe I've used wrong terms in my query; apologies). Considering the example in the above mentioned link(having green tick mark), I want to create a heading, in each individual sheet, for each of the tables generated (I'm not referring to naming the individual sheets). How do I get the headings for each table(present in each sheet)? Hope I'm clear now.Thanks a lot.Abhilash
So you want to add a row, and then add text in a contiguous block of cells within that row? (Still no code or example data.)IRTFM
I've edited my question, adding an example. In the example, how would I be able to add titles in each sheet(Credit card details...in sheet1,Population data..in sheet2 and M/S ABC Limited...in sheet3)? (Appreciate your help very much. Thank you.)Abhilash

1 Answers

2
votes

You have to work with writeData() function of openxlsx package:

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")