问题
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