openxlsx 无法读取 R 中的 .xlsx 文件

openxlsx not able to read from .xlsx file in R

我正在尝试使用 R 中的 openxlsx 包从 .xlsx 文件中读取值。简而言之,我需要写入一行数据,然后填充一些必须在 R 中读回的输出单元格。我将分享一个示例以更好地解释问题。

.xlsx 文件的初始状态:

我现在正在尝试向单元格写入新值:A2:A3 = c("c", 5)。所以理想情况下,我期待 A6 = 15

下面是使用的代码:

require(openxlsx)
path <- "C:/path_to_file/for_SO1.xlsx"
input_row <- c("c", 5)
# Load workbook; create if not existing
wb <- loadWorkbook(path)
# createSheet(wb, name = "1")
writeData(wb, 
          sheet = "Sheet1",
          x = data.frame(input_row),
          startCol=1,
          startRow=1
)  

data_IM <- read.xlsx(wb, 
                     sheet = "Sheet1",
                     rows = c(5,6),
                     cols = c(1))
# Save workbook
saveWorkbook(wb, file = path, overwrite = TRUE)

#> data_IM
#  output_row
#1          3

但是我得到了初始值(3)。但是,如果我打开 .xlsx 文件,我可以看到 15 驻留在那里:

无法读取此单元格的原因可能是什么?我尝试在写入文件后保存它并再次读取它,但即使这样也失败了。 openxlsx 是我唯一的选择,因为 JAVA 来自 XLConnect 等的错误

?read.xlsx

Formulae written using writeFormula to a Workbook object will not get picked up by read.xlsx(). This is because only the formula is written and left to be evaluated when the file is opened in Excel. Opening, saving and closing the file with Excel will resolve this.

所以文件需要在Excel中打开然后保存,我可以验证这是否有效。然而这可能不适合你。

XLConnect 似乎具有所需的功能

# rjava can run out of memory sometimes, this can help.
options(java.parameters = "-Xmx1G")
library(XLConnect)

file_path = "test.xlsx"

input_row <- c("c", 5)

wb <- loadWorkbook(file_path, create=F)
writeWorksheet(wb, 1, startRow = 1, startCol = 1, data = data.frame(input_row))
setForceFormulaRecalculation(wb, 1, TRUE)
saveWorkbook(wb)

# checking
wb <- loadWorkbook(file_path, create=F)
readWorksheet(wb, 1)

文件https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf

read.xlsx() 不会选取工作簿对象。 这是因为在 Excel 中打开文件时只写入公式并留待计算。 使用 Excel 打开、保存和关闭文件将解决此问题。 所以如果你使用 windows 那么 将以下文件 vbs 文件保存到例如 opensaveexcel.vbs

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\Book2.xlsx")
objWorkbook.Save
objWorkbook.Close 
objExcel.Quit
Set objExcel = Nothing
Set objWorkbook = Nothing

然后你可以编写 R 代码,因为单元格 A4 在 Book1.xlsx 中有公式 =A3*5

mywritexlsx(fname="d:/Book1.xlsx",data = 20,startCol = 1,startRow = 3)
system("cp d:\Book1.xlsx d:\Book2.xlsx")
system("cscript //nologo d:\opensaveexcel.vbs")
tdt1=read.xlsx(xlsxFile = "d:/Book1.xlsx",sheet = "Sheet1",colNames = FALSE)
tdt2=read.xlsx(xlsxFile = "d:/Book2.xlsx",sheet = "Sheet1",colNames = FALSE)

mywritexlsx 对我有用

mywritexlsx<-function(fname="temp.xlsx",sheetname="Sheet1",data,
                  startCol = 1, startRow = 1, colNames = TRUE, rowNames = FALSE)
{
  if(!file.exists(fname))
  {
   wb = openxlsx::createWorkbook()
   sheet = openxlsx::addWorksheet(wb, sheetname)
  }
  else
 {
   wb <- openxlsx::loadWorkbook(file =fname)
   if(!(sum(openxlsx::getSheetNames(fname)==sheetname)))
   sheet = openxlsx::addWorksheet(wb, sheetname)
   else
    sheet=sheetname
  }

  openxlsx::writeData(wb,sheet,data,startCol = startCol, startRow = startRow, 
          colNames = colNames, rowNames = rowNames)
  openxlsx::saveWorkbook(wb, fname,overwrite = TRUE)
}