I know it is possible to produce xlsx workbook using R(OpenXlSX)
But is there a way to produce xls workbook using R ?
I know export is possible but for a xls sheet but I need a workbook.
You can also convert a xlsx file to a xls file with the following code :
library(RDCOMClient)
path_Excel_File1 <- "D:\\file1.xlsx"
path_Excel_File_Output <- "D:\\file1.xls"
xlApp <- COMCreate("Excel.Application")
xlWbk1 <- xlApp$Workbooks()$Open(path_Excel_File1)
xlWbk1$SaveAs(path_Excel_File_Output, -4143) # saving as .xls
Related
I'm planning on using R markdown to generate summary one-pager PDF's from primary data stored in Excel spreadsheets. My collaborators are already familiar with the plots that are created in these spreadsheets so I don't want to re-create them. Is there an easy way to pull these existing plots out of the Excel spreadsheets as an image and drop them into the R markdown report?
I was able to export a chart from Excel with the following code : (see How to export an Excel sheet range to a picture, from within R)
library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Graph Excel_Test2.xlsx")
xlScreen <- 1
xlBitmap <- 2
xlWbk$ActiveSheet()$ChartObjects(1)$Activate()
graph <- xlWbk$ActiveChart()$SeriesCollection(1)
graph$Select()
oCht <- xlApp$ActiveChart()
oCht$Paste()
oCht$Export("C:\\SavedRange_Test2.jpg", "JPG")
# CLOSE WORKBOOK AND APP
xlWbk$Close(FALSE)
xlApp$Quit()
# RELEASE RESOURCES
oCht <- xlWbk <- xlApp <- NULL
rm(oCht, xlWbk, xlApp)
gc()
After, you simply have to import the JPG in an Rmarkdown.
I am trying to manipulate an Excel file (.xls) in R through RDCOMClient Package.
I created an Excel object in R, opened a workbook saved as .xls file format, and tried to convert the file format into .xlsx without pop-up dialog box when there is an Excel file with the same file name. Codes as below.
excel <- COMCreate("Excel.Application")
wb <- excel$Workbooks()$Open(Filename = "filepath.xls",Password = "xxxxx")
excel$DisplayAlerts(FALSE)
wb$SaveAs(Filename = "filepath.xlsx" ,FileFormat = 51,Password = "")
I got an error message when I executed the code:
excel$DisplayAlerts(FALSE)
<'checkErrorInfo'> 8002000E Error: invalid number of parameter.
You should replace it with the following:
excel[["DisplayAlerts"]]=FALSE
I get the corruption error when I try to open the Excel workbook created in R.
I tried with both .xlsx and .xls extensions but neither worked!
The code that I used for doing all this is:
wb <- loadWorkbook("RCreated.xls", create = TRUE);
saveWorkbook(wb)
createSheet(wb, name = "First")
HELP!
Create the sheet BEFORE saving the workbook.
I can open an excel workbook from within R using:
shell.exec("abc.xlsx")
but how can I open the workbook at a specific worksheet using shell exec?
Thank you for your help
This does not use shell.exec but does open the specified sheet:
# start Excel
library(RDCOMClient)
xl <- COMCreate("Excel.Application")
xl[["Visible"]] <- TRUE
# activate second sheet of indicated xlsx file
file <- normalizePath("abc.xlsx")
sheetNo <- 2
xl[["Workbooks"]]$Open(file)$Sheets(sheetNo)$Activate()
To specify sheet name instead of number replace last two lines with:
sheetName <- "Sheet2"
xl[["Workbooks"]]$Open(file)$Sheets(sheetName)$Activate()
I am trying to change the page setting of a xlsx file so that it will be printed as landscape, but not portrait, I tried the following:
library(xlsx)
wb <- createWorkbook()
sheet <- createSheet(wb, "Sheet1")
ps <- printSetup(sheet, landscape=TRUE, copies=3)
This is OK if I create a new Excel workbook, but I can't use it when I am using the loadWorkbook function to load an xls file. I wonder why.
Update: I am working on a xls file but not xlsx file and found the answer below cannot solve my problem, any further suggestions? Thanks.
I can get it to work by using getSheets to select the sheet to set the print area for:
wb <- loadWorkbook("test.xlsx")
printSetup(getSheets(wb)[[1]], landscape=TRUE, copies=3)
saveWorkbook(wb,"test.xlsx")