Overwrite An Excel File through RDCOMClient Package in R - r

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

Related

Exporting .XLS file from openxlsx

I have a function that receives DataFrame does a bunch of transformations with openxlsx and exports the data from R to .xlsx:
export_workbook_from_df <- function(data, path) {
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, sheetName = "Sheet1")
openxlsx::openxlsx_setOp("numFmt", "0,00")
number_format <- openxlsx::createStyle(numFmt = "Number") # create thousands format
wb |>
openxlsx::addStyle(sheet = 1,
number_format,
rows = 1:nrow(dados) + 1, cols = c(6),
gridExpand = T
)
openxlsx::writeData(wb, sheet = 1, data)
openxlsx::saveWorkbook(wb, paste0(path, ".xlsx"))
}
if I try to save as .xls using openxlsx::saveWorkbook(wb, paste0(path, ".xls")) I get the following error:
Which roughly translates to:
The format of the file and the extension don't correspond. The file may be corrupted or not be safe. Don't open it, unless you trust the source. Do you want to open anyway?
The file works fine if I save it as .xlsx and manually save as .xls within Excel;
I also tried using XLConnect to load the file after is saved and export in a different format, like:
openxlsx::saveWorkbook(wb, paste0(path, ".xlsx"))
XLConnect::loadWorkbook(paste0(path, ".xlsx")) %>%
XLConnect::saveWorkbook(paste0(path, ".xls"))
While it does export the file as .xls I get the same error.
It may be worth mentioning that when I open the file I get exactly the same data as in the .xlsx file when using either methods (using openxlsx and XLConnect)
The xls and xlsx file formats are not the same: XLSX is a zipped, XML-based file format. Microsoft Excel 2007 and later uses XLSX as the default file format when creating a new spreadsheet. Support for loading and saving legacy XLS files is also included. XLS is the default format used with Office 97-2003. When you try to load the XLSX which you saved as an XLS Excel barfs as above because it is expecting the old binary format but it is encountering a zipped XML-based one instead.

Export Xls workbook using r

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

How can I load an xlsx workbook into R?

I have an xlsx workbook on my desktop with existing sheets. I have a dataframe in R that I would like to add to this workbook as a new sheet.
Using library(openxlsx) I want to do:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
addWorksheet(wb, "New_Sheet")
writeData(wb, "New_Sheet", df)
saveWorkbook(wb, "workbook.xlsx", overwrite=TRUE)
however the program fails at the first line:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
I get the error message:
Error in match(sheetrID, file_rIds): object sheetrId not found
I also created a dummy file (excel workbook with just two existing sheets and some dummy strings in each sheet) and I get the same error message.
I also tried uninstalling openxlsx and re-installing. No change.
As mentioned in the comments by #MrFlick (thank you!), Excel files are actually zipped files that contain XML files. So the workbook must be unzipped before it can be opened in R.

R readxl::read_excel failed to open xls file

readxl_1.1.0
I'm trying to read the file from this link (US gov website)
https://www.cftc.gov/files/dea/history/dea_com_xls_2018.zip
When I unzip the xls file inside, and read with readxl::read_excel, it fails with the error message failed to open C:\path to file
I can open the file in excel, save it to csv and read it to R by fread, but there are a lot of those files, so that's tedious. By the way, some other xls files downloaded from the same webpage can be read by read_excel
There's something odd about the xls file. I think it's because it contains some VBA code.
If you are happy to use XLConnect here is an alternative that reads the file.
library(XLConnect)
extdir = tempdir()
unzip("dea_com_xls_2018.zip", exdir = extdir)
file = list.files(extdir, pattern = 'xls', full.names = T)
wb = loadWorkbook(file)
ws = readWorksheet(wb, sheet = 1)
dim(ws)
#[1] 11131 126

Can't open Excel File created in R language

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.

Resources