在 openxlsx 包 R 中使用 mergeCells() 会在输出中产生错误
Using mergeCells() in openxlsx package R creates error in output
我正在尝试将 R 中的数据帧输出到 excel,但在尝试打开生成的 xlsx 文件时使用 mergeCells() 时,我一直收到错误消息。虽然单元格确实合并,但我的数据是 "lost." 我可以取消合并单元格并且数据在那里但我想对其进行格式化以便输出(例如我的 df 的第 1 列)跨越多个列。
我试过在将数据写入工作表之前和之后合并单元格。我也试过使用 writeDataTable() 和 writeData(),两者都不起作用。我试过在不同的列上启动 df(如下所示)。例如,开始将 df 写入第 2 列,并合并列 1:2。另一个我先合并列 1:2 然后从第 1 列开始写入数据。
df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)
book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)
或
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)
保存后打开文件,报错"We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."
感谢任何帮助!
如果我对您的问题的理解正确,您希望合并第 1 列和第 2 列以生成包含 df
第一列的合并列。如果这是正确的,那么您的问题是您要合并到的最左边的列(第 1 列)是空白的。 openxlsx::mergeCells()
然后将合并最左侧列的内容。要合并第 1 列和第 2 列并在其中包含 df[1]
的内容,您需要在最左侧的列中写入 df[1]
的内容,如下所示:
library(openxlsx)
df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)
wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook
我正在尝试将 R 中的数据帧输出到 excel,但在尝试打开生成的 xlsx 文件时使用 mergeCells() 时,我一直收到错误消息。虽然单元格确实合并,但我的数据是 "lost." 我可以取消合并单元格并且数据在那里但我想对其进行格式化以便输出(例如我的 df 的第 1 列)跨越多个列。
我试过在将数据写入工作表之前和之后合并单元格。我也试过使用 writeDataTable() 和 writeData(),两者都不起作用。我试过在不同的列上启动 df(如下所示)。例如,开始将 df 写入第 2 列,并合并列 1:2。另一个我先合并列 1:2 然后从第 1 列开始写入数据。
df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)
book <- createWorkbook()
sheet <- "Sheet1"
writeData(book, sheet, df, startCol = 2, startRow = 1, colNames = TRUE)
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
saveWorkbook(book)
或
mergeCells(book, sheet, cols = 1:2, rows = 1)
mergeCells(book, sheet, cols = 1:2, rows = 2)
mergeCells(book, sheet, cols = 1:2, rows = 3)
writeDataTable(book, sheet, df, startCol = 1, startRow = 1, colNames = TRUE)
saveWorkbook(book)
保存后打开文件,报错"We found a problem with some content, etc. Excel was able to open the file by removing or repairing unreadable content."
感谢任何帮助!
如果我对您的问题的理解正确,您希望合并第 1 列和第 2 列以生成包含 df
第一列的合并列。如果这是正确的,那么您的问题是您要合并到的最左边的列(第 1 列)是空白的。 openxlsx::mergeCells()
然后将合并最左侧列的内容。要合并第 1 列和第 2 列并在其中包含 df[1]
的内容,您需要在最左侧的列中写入 df[1]
的内容,如下所示:
library(openxlsx)
df <- data.frame(
Category = c("A", "B", "C"),
Type = c("x", "y", "z"),
Number = c("1", "2", "3"), stringsAsFactors = FALSE)
wb <- createWorkbook() # creates workbook
addWorksheet(wb, "Sheet1") # adds sheet
writeData(wb, 1, df[1], startCol = 1, startRow = 1, colNames = TRUE) # writing content on the left-most column to be merged
writeData(wb, 1, df[2:3], startCol = 3, startRow = 1, colNames = TRUE) # write the rest of the content on the columns that wont be merged
for(i in seq_len(nrow(df) + 1)){ # loop over rows for merging
mergeCells(wb, 1, cols = 1:2, rows = i)
}
rm(i)
saveWorkbook(wb, "test.xlsx", overwrite = T) # save workbook