Excel is the de facto standard format for enterprise spreadsheet processing. R itself handles various text formats, such as CSV, or its own binary format files. For two groups of people to be able to communicate with each other, R must be able to read the XLSX format.

The R package dedicated to Excel files is XLSX.

Online Installation

> install.packages("xlsx"Also installing the dependencies' rJava ', 'xlsxjars' trying URL'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.6/rJava_0.9-11.zip'
Content type 'application/zip' length 832080 bytes (812 KB)
downloaded 812 KB

trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.6/xlsxjars_0.6.1.zip'
Content type 'application/zip' length 9485571 bytes (9.0 MB)
downloaded 9.0 MB

trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.6/xlsx_0.6.1.zip'
Content type 'application/zip' length 460695 bytes (449 KB)
downloaded 449 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

The downloaded binary packages are in
	C:\Users\chang\AppData\Local\Temp\RtmpqE36kp\downloaded_packages
Copy the code

Try loading the package

> library(xlsx)
Error: package or namespace load failed for'XLSX: loadNamespace ()'rJava'Error: JAVA_HOME cannot be determined from the RegistryCopy the code

Error reported unexpectedly.

Let’s see, it turns out that this package depends on Java. No way, although there is no Java software in hand, but also to install a. That doesn’t include Android phones, of course.

Install the Java

Go directly to Java· official website: www.java.com/zh_CN/

Good old classic web page. Click the bright “Free Java Download” button.

Install the default 64-bit Java SE directly.

The installation process is also a classic Windows software installation process. There shouldn’t be any trouble.

I am so bad. I can’t believe I clicked it and then disappeared. Restart and run as administrator to start the installation properly.

Import the XLSX package again

> library(xlsx)
Copy the code

No error, that’s a success.

Using XLSX package

So let’s go to the code

> table1_1 <- read.xlsx2("e:/example/ch1/table1_1.xlsx".1)
> table1_1
Copy the code

Normal operation. It seems to be a little off, use fix to check again, there is no problem. That was a display problem.

Parameter 1 refers to the index of the sheet in the XLSX file. Attention! Excel tables are indexed from 1. Of course, you can use the table name instead.

> table1_1 <- read.xlsx2("e:/example/ch1/table1_1.xlsx"."table1_1")
> table1_1
Copy the code

Read. Xlsx2 is preferred

Read. Xlsx2 is said to perform better than read. XLSX, or some things R is much slower than Java. 😂

Quantitative investment and futures foreign exchange scattered fairy, fund insurance level is also taken out