使用openxlsx将数据框格式化为Excel。

huangapple go评论96阅读模式
英文:

Format dataframe to Excel using openxlsx

问题

以下是您要翻译的内容:

"Any suggestion how can i deal with the two first rows? My data frame actually is just the second row (which has the title of the columns) to the bottom. But i don't have any idea how to add the first line

使用openxlsx将数据框格式化为Excel。

I've tried to pack two data frames:

addWorksheet(wb, "sheet2")
auxdata = tibble(TEST = rep('',nrow(base_original)))

writeData(wb, sheet = 3, x = auxdata)
writeData(wb, sheet = 3, x = df1)

but didn't work of course"

英文:

Any suggestion how can i deal with the two first rows? My data frame actually is just the second row (which has the title of the columns) to the bottom. But i don't have any idea how to add the first line

使用openxlsx将数据框格式化为Excel。

I've tried to pack two data frames:

addWorksheet(wb, "sheet2")
auxdata = tibble(`TEST` = rep('',nrow(base_original)))

writeData(wb, sheet = 3, x = auxdata)
writeData(wb, sheet = 3, x = df1)

but didn't work of course

答案1

得分: 0

writeData有一个startRow(和startCol)参数来指定要写入数据的区域。因此,您可以设置startRow=2来写入您的数据集。然后在第一行添加额外的标题行:

library(openxlsx)

fn <- tempfile(".xlsx")
df1 <- mtcars[, 1:2]

wb <- createWorkbook()
addWorksheet(wb, "sheet2")
writeData(wb, sheet = "sheet2", x = "TEST")
writeData(wb, sheet = "sheet2", x = df1, startRow = 2)
mergeCells(wb, sheet = "sheet2", cols = 1:2, rows = 1)

saveWorkbook(wb, fn)

使用openxlsx将数据框格式化为Excel。

英文:

writeData has a startRow (and startCol) argument to specify the region to write the data. Hence, you can set startRow=2 to write your dataset. Then add the additional header line in the first row:

library(openxlsx)

fn &lt;- tempfile(&quot;.xlsx&quot;)
df1 &lt;- mtcars[, 1:2]

wb &lt;- createWorkbook()
addWorksheet(wb, &quot;sheet2&quot;)
writeData(wb, sheet = &quot;sheet2&quot;, x = &quot;TEST&quot;)
writeData(wb, sheet = &quot;sheet2&quot;, x = df1, startRow = 2)
mergeCells(wb, sheet = &quot;sheet2&quot;, cols = 1:2, rows = 1)

saveWorkbook(wb, fn)

使用openxlsx将数据框格式化为Excel。

huangapple
  • 本文由 发表于 2023年7月14日 09:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定