将.xls转换为.txt,反之亦然

时间:2021-02-02 22:51:26

I want to know if conversion of file format is possible in R. If i have a .xls file and would like to convert to a .txt format or viceversa. likewise is conversion of other formats possible .xls to .csv and so on.

我想知道在R中是否可以转换文件格式。如果我有.xls文件并想转换为.txt格式或反之亦然。同样是将其他格式转换为.xls到.csv等等。

1 个解决方案

#1


1  

Read in the .xls file with whatever package you like. I use openxlsx, but use xlsx for that specific file type.

使用您喜欢的任何包读入.xls文件。我使用openxlsx,但使用xlsx作为特定的文件类型。

library(xlsx)
data <- read.xlsx2(file, sheet, ...)

write.table(data, "filename.txt", ...) 

#or

write.csv(data, "filename.csv", ...)

Or if you want a function, try something like:

或者,如果您想要一个功能,请尝试以下方法:

library(xlsx)
xls.csv.converter <- function(File, Sheet=1) {
d <- read.xlsx2(File, Sheet)
write.csv(d, paste0(getwd(), "/", substr(File, 1, nchar(File)-4),".csv"))
}

xls.csv.converter("mydata.xls")

Note that the function search for the file in your working directory and the file will be written to your working directory.

请注意,工作目录中文件的功能搜索和文件将写入您的工作目录。

#1


1  

Read in the .xls file with whatever package you like. I use openxlsx, but use xlsx for that specific file type.

使用您喜欢的任何包读入.xls文件。我使用openxlsx,但使用xlsx作为特定的文件类型。

library(xlsx)
data <- read.xlsx2(file, sheet, ...)

write.table(data, "filename.txt", ...) 

#or

write.csv(data, "filename.csv", ...)

Or if you want a function, try something like:

或者,如果您想要一个功能,请尝试以下方法:

library(xlsx)
xls.csv.converter <- function(File, Sheet=1) {
d <- read.xlsx2(File, Sheet)
write.csv(d, paste0(getwd(), "/", substr(File, 1, nchar(File)-4),".csv"))
}

xls.csv.converter("mydata.xls")

Note that the function search for the file in your working directory and the file will be written to your working directory.

请注意,工作目录中文件的功能搜索和文件将写入您的工作目录。