R read_excel: libxls error: Unable to parse file

和自甴很熟 提交于 2021-02-11 14:31:18

问题


I'm trying to read xls file into R using readxl::read_excel, but it's giving the following errors:

Error: 
  filepath: //.../data.xls
  libxls error: Unable to parse file

also tried readxl::excel_sheets(), but same errors.

  • readxl::format_from_ext(path) gives "xls"

Also tried re-installing readxl packages, didn't work for me.

My current alternative is to convert this file to "xlsx" using Excel and then read in with readxl::read_excel, but I would like to import the "xls" directly.

How to fix it?


回答1:


I've come across this problem a lot (exact same error message) and it seems to be something to do with the way the .xls file has been saved or exported. Eg. in excel if you inspect the sheet it sometimes turns up warnings. However, inspection or re-saving is not practical when you have automated files, or a huge number of files which you need opened by R. I have some example .xls files that fail to be loaded by readxl, openxlsx libraries, no matter what options I try (for example, specifying sheet, which often solves the problem of loading excel sheets). I have solved this problem finally, by simply using the XLConnect package (note that it does require Java to be installed). You don't have a sample file, and I can't share the ones I am working on, but try this

install.packages('XLConnect')
library(XLConnect)
# to load sheet one of 'test.xls', and my sheet does not have a header
# method one, read workbook, followed by worksheet
f <- loadWorkbook('test.xls')
d <- readWorksheet(f, 1, header = F)
# method two, read sheet directly into your dataframe
d <- readWorksheetFromFile ('test.xls', 1, header = F)


来源:https://stackoverflow.com/questions/60609875/r-read-excel-libxls-error-unable-to-parse-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!