2015-04-12 122 views
2

我想将保存为启用宏的工作簿(Jimmy.xlsm)保存为Jimmy.xlsx的Excel文件(称其名称为“Jimmy”)。在R中将.xlsm转换为.xlsx

我需要在编码环境中完成此操作。我不能简单地通过在Excel中打开文件并分配不同的文件类型来改变这一点。我目前正在编程R.如果我使用功能

file.rename("Jimmy.xlsm", "Jimmy.xlsx") 

该文件变得损坏。

+1

[百万种连接R和excel的方式](http://www.thertrader.com/2014/02/11/a-million-ways-to-connect-r-and-excel/)值得一读取,以及[xlsx包是否适用于R中的xlsm文件?](http://stackoverflow.com/a/11215451/4002530) – tospig

回答

1

在您的框架中,您必须在表格中阅读并将其写回。假设你有一个名为“testXLSM2X.xlsm”的XLSM文件(包含宏,我假设)包含一个带有表格列数据的工作表。这将做到这一点:

library(xlsx) 
r <- read.xlsx("testXLSMtoX.xlsm", 1) # read the first sheet 
# provides a data frame 
# use the first column in the spreadsheet to create row names then delete that column from the data frame 
# otherwise you will get an extra column of row index numbers in the first column 
r2w<-data.frame(r[-1],row.names=r[,1]) 
w <- write.xlsx(r2w,"testXLSMtoX.xlsx") # write the sheet 

当然,宏将被剥离出来。

这是一个答案,但我会质疑你在努力完成什么。一般来说,从Excel中控制R比从R中控制Excel更容易。我使用来自http://rcom.univie.ac.at/的REXCEL,它不是开源的,但非常强大。