蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

生成真实工作簿并复读多张工作表

RWindows x64 R 4.6.2,xlsx 0.6.5、rJava 1.0-18、Java 8 和 readxl 1.6.0。excel.R
excel.R51 行
emit <- function(key, values) {
  cat(key, "=", paste(values, collapse = ","), "\n", sep = "")
}
main <- function() {
  workspace <- tempfile("mbsky-excel-")
  dir.create(workspace)
  on.exit(unlink(workspace, recursive = TRUE), add = TRUE)
  installed <- rownames(installed.packages())
  emit("installed", c("xlsx" %in% installed, "readxl" %in% installed, "package:xlsx" %in% search()))
  suppressPackageStartupMessages(library("xlsx"))
  emit("loaded", c("package:xlsx" %in% search(), "rJava" %in% loadedNamespaces(),
                    "xlsxjars" %in% loadedNamespaces(),
                    as.character(packageVersion("xlsx")), as.character(packageVersion("readxl"))))
  samples <- data.frame(code=c("007","019","105"), station=c("Ridge","Lake","Bay"),
                        count=c(18,23,6), stringsAsFactors=FALSE)
  notes <- data.frame(field="unit", label="visits")
  xlsx_path <- file.path(workspace, "stations.xlsx")
  xls_path <- file.path(workspace, "stations.xls")
  save_workbook <- function(format, path) {
    workbook <- createWorkbook(type = format)
    addDataFrame(samples, createSheet(workbook, sheetName = "Stations"), row.names = FALSE)
    addDataFrame(notes, createSheet(workbook, sheetName = "Metadata"), row.names = FALSE)
    header <- createSheet(workbook, sheetName = "With title")
    title_cell <- createCell(createRow(header, rowIndex = 1), colIndex = 1)[[1,1]]
    setCellValue(title_cell, "Field report")
    addDataFrame(notes, header, startRow = 2, row.names = FALSE)
    saveWorkbook(workbook, path)
  }
  save_workbook("xlsx", xlsx_path)
  save_workbook("xls", xls_path)
  types <- c("character", "character", "numeric")
  first <- read.xlsx(xlsx_path, sheetIndex = 1, colClasses = types)
  old <- read.xlsx(xls_path, sheetName = "Stations", colClasses = types)
  second <- read.xlsx(xlsx_path, sheetIndex = 2, colClasses = c("character", "character"))
  emit("legacy_read", c(nrow(first), ncol(first), first$code, sum(first$count),
                       isTRUE(all.equal(first, old)), second$label))
  modern <- readxl::read_excel(xlsx_path, sheet = "Stations", col_types = c("text","text","numeric"))
  modern_old <- readxl::read_excel(xls_path, sheet = 1, col_types = c("text","text","numeric"))
  small <- readxl::read_excel(xlsx_path, range = "Stations!A1:B2", col_types = "text")
  emit("modern_read", c(readxl::excel_sheets(xlsx_path), nrow(modern), ncol(modern),
                       modern$code, typeof(modern$count), sum(modern$count),
                       isTRUE(all.equal(modern, modern_old)), small$code))
  offset <- read.xlsx(xlsx_path, sheetName = "With title", startRow = 2,
                      colClasses = c("character", "character"))
  offset_modern <- readxl::read_excel(xlsx_path, sheet = "With title", skip = 1,
                                      col_types = "text")
  emit("header_offset", c(names(offset), offset$label, offset_modern$label))
  emit("java", rJava::.jcall("java/lang/System", "S", "getProperty", "java.version"))
}
main()
本例需在 Windows x64 R 4.6.2,xlsx 0.6.5、rJava 1.0-18、Java 8 和 readxl 1.6.0。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64 R 4.6.2,xlsx 0.6.5、rJava 1.0-18、Java 8 和 readxl 1.6.0。

入口文件:projects/r-package-and-tabular-io/excel.R

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 excel.R,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64 R 4.6.2,xlsx 0.6.5、rJava 1.0-18、Java 8 和 readxl 1.6.0。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:R

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip

下载课程关联工程包:R

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a326.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习