如何使用R导出为.xls excel格式

yc0p9oo0  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(162)

请,我有这个数据框,我想导出它在Excel“.xls”文件格式。

df1 <- data.frame(sales_value = c(532489, 20000, 32111, 407536),
                  name = c("John", "Jane", "Jim", "Joan"))

我试过这个代码。

write.table(df1, file = "df1.xls", row.names = FALSE, sep = "\t")

但它包括销售价值中不需要的小数。
我试了下面的代码。

write.xlsx(df1, "df.xls")

But the output exported excel file was internally a .xlsx file format.

Please is there any other way of doing it?

ldioqlga

ldioqlga1#

可以考虑以下方法。此方法仅适用于Windows。

library(RDCOMClient)
library(openxlsx)

df1 <- data.frame(sales_value = c(532489, 20000, 32111, 407536),
                  name = c("John", "Jane", "Jim", "Joan"))

write.xlsx(df1, "D:\\df1.xlsx")

path_Excel_File <- "D:\\df1.xlsx"
path_Excel_File_Output <- "D:\\df1.xls"

xlApp <- COMCreate("Excel.Application")
xlWbk1 <- xlApp$Workbooks()$Open(path_Excel_File)

xlWbk1$SaveAs(path_Excel_File_Output, -4143) # Save as XLS

相关问题