将数据导出到Excel工作表中的特定单元格 [英] Export data into specific cells in Excel sheet

查看:269
本文介绍了将数据导出到Excel工作表中的特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将R中的数据框导出到Excel工作表,可以通过以下方式完成:

I need to export a data frame in R to an Excel sheet, which can be done via:

require("xlsx")
write.xlsx(x, file, sheetName="Sheet1", col.names=TRUE, 
    row.names=TRUE, append=FALSE, showNA=TRUE)

然后,我导出的数据将粘贴到Excel工作表的"A1"单元格中.我还可以设置append=TRUE,以便将数据添加到工作表的末尾.

My exported data will then be pasted beginning in cell "A1" of the Excel sheet. I can also set append=TRUE so the data will be added at the end of the sheet.

是否可以将其放入特定单元格?我需要将数据帧导出到以单元格C10开始的范围.这可能吗?

Is there a possibility to put it into a specific cell? I need to export the data frame to a range beginning at cell C10. Is this possible?

更新: 该工作表包含行1-9和列A-B中的数据.因此,不可能仅将空单元格添加到数据框中并将其粘贴到excel工作表中,因为这些空单元格会擦除数据.

Update: The sheet contains data in rows 1-9 as well as columns A-B. Therefore it is not possible to just add empty cells to the data frame and paste it into the excel sheet, because those empty cells would erase the data.

推荐答案

您可以使用包XLConnect来实现.

You can do it with the package XLConnect.

library(XLConnect)
wb <- loadWorkbook("File_result.xlsx"), create = TRUE)
createSheet(wb, name = "Sheet1")

# here, you can set the startRow and startCol. Col A = 1, B = 2,...
writeWorksheet(wb,x,"Sheet1",startRow = 10, startCol = 3, header = TRUE)

# automatically adjust column width
setColumnWidth(wb, sheet = "Sheet1", column = 3:4, width = -1)
saveWorkbook(wb)

这篇关于将数据导出到Excel工作表中的特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆