I want to print an Excel
file to a pdf
file after manipulating it. For the manipulation I used the .xlsx
package which works fine. There is a function printSetup
but I cannot find a function to start the printing. Is there a solution for this?
library(xlsx)
file <- "test.xlsx"
wb <- loadWorkbook(file)
sheets <- getSheets(wb) # get all sheets
sheet <- sheets[[1]] # get first sheet
# HERE: MAGIC TO SAVE THIS SHEET TO PDF
It may be a solution using DCOM through the RDCOMClient
package, though I would prefer a plattform independent solution (e.g. using xlsx
) as I work on MacOS. Any ideas?
Below a solution using the DCOM
interface via the RDCOMClient
. This is not my preferred solution as it only works on Windows. A plattform independent solution would still be appreciated.
library(RDCOMClient)
library(R.utils)
file <- "file.xlsx" # relative path to Excel file
ex <- COMCreate("Excel.Application") # create COM object
file <- getAbsolutePath(file) # convert to absolute path
book <- ex$workbooks()$Open(file) # open Excel file
sheet <- book$Worksheets()$Item(1) # pointer to first worksheet
sheet$Select() # select first worksheet
ex[["ActiveSheet"]]$ExportAsFixedFormat(Type=0, # export as PDF
Filename="my.pdf",
IgnorePrintAreas=FALSE)
ex[["ActiveWorkbook"]]$Save() # save workbook
ex$Quit() # close Excel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With