在R中打开/读取Excel文件

时间:2018-07-29 05:05:51

标签: r excel readxl

我无法使用readxl打开/读取从澳大利亚统计局网站下载的excel文件。

我已经从网站上downloaded Table 12了,但是当我去阅读r中的工作簿表时,我收到一条错误消息:

library(readxl)    
excel_sheets(path = "C:/Users/Name/Documents/downloaded_file.xls")

"Error in xls_sheets(path) : Failed to open C:/Users/Name/Documents/downloaded_file.xls".

readxl的早期版本中,我很容易将这些文件读取到r中,但是经过几个月的中断,我最近更新了自己的readxl版本,但现在不起作用了。

我尝试使用download.file函数下载文件,但要小心设置mode = wb,但这对访问工作簿中的数据也没有影响。

感谢任何指针。

2 个答案:

答案 0 :(得分:0)

您是否尝试过其他软件包。如果我手动下载文件并使用xlsx进行读取,那么它对我有用。在ouptut下方是您需要的东西,还是我缺少什么?

library("xlsx")
# there is certainly a better (faster) way to get the sheet number
n_sheets =  length(getSheets(loadWorkbook("6202012.xls")))
# if you know which sheet to load, reading the sheet works for me...
df = read.xlsx("6202012.xls", 2)
df[1:3, 1:3]
#            NA. Employed.total....Persons....Australia.. Employed.total....Persons....Australia...1
# 1        Unit                                      000                                        000
# 2 Series Type                                    Trend                        Seasonally Adjusted
# 3   Data Type                                    STOCK                                      STOCK

答案 1 :(得分:0)

它在Windows上适用:

library(readxl)    
excel_sheets(path = "C:/Users/Name/Documents/downloaded_file.xls")
data<-readxl::read_excel(path = ""C:/Users/Name/Documents/downloaded_file.xls"",sheet = "Data1")