Search code examples
javarexcelr-xlsx

Error opening Excel file in R using xlsx


I recently installed R 3.2.0 (Windows 64 bit) with packages xlsx, which depends on rJava and xlsxjars. Java 1.8.0_11 64 bit is installed on the machine.

When I try to use the read.xlsx() function, I get the following error about a zipfile:

> library(xlsx)
Loading required package: rJava
Loading required package: xlsxjars
> read.xlsx("cameras.xlsx", sheetIndex=1)

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.util.zip.ZipException: invalid distance too far back

I sure don't know why it would be trying to unzip anything, unless it is one of the jars that comes with the R packages. (The zip files that the R packages came in unzip fine when I do it by hand.) I've re-installed twice to no avail in case of a corruption there.

I feel like there's a config error somewhere, but I'm really stumped. Has anyone else experienced this? Here are the package install messages

> install.packages("xlsx")
Installing package into ‘C:/Users/ggraham412/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
also installing the dependencies ‘rJava’, ‘xlsxjars’

trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/rJava_0.9-6.zip' 
Content type 'application/zip' length 761928 bytes (744 KB)
downloaded 744 KB

trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/xlsxjars_0.6.1.zip'
Content type 'application/zip' length 9485172 bytes (9.0 MB)
downloaded 9.0 MB

trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/xlsx_0.5.7.zip'
Content type 'application/zip' length 400857 bytes (391 KB)
downloaded 391 KB

package ‘rJava’ successfully unpacked and MD5 sums checked
package ‘xlsxjars’ successfully unpacked and MD5 sums checked
package ‘xlsx’ successfully unpacked and MD5 sums checked

Solution

  • Turns out it was the problem described here: R xlsx package error

    Didn't know that xlsx file was accessed by zip library in the above process, and indeed I did not download the spreadsheet as binary.