Hello,
I have two Data Frames which has headers and data in them, and I want to write data from these two Data Frames into one single excel sheet. I know we can write each data frame data into separate sheet. Any advice?
julia> df
5×2 DataFrame
Row │ A B
│ Int64 String
─────┼───────────────
1 │ 1 M
2 │ 2 F
3 │ 3 F
4 │ 4 M
5 │ 1 0
julia> df2
2×2 DataFrame
Row │ AA AB
│ String Float64
─────┼─────────────────
1 │ aa 10.1
2 │ bb 10.2
The "rw" mode is a little dangerous, especially if your Excel document contains formulas (reference). Also, the do block form is more robust; it will always automatically close the file.
This is how I would do it.
using DataFrames, XLSX
df1 = DataFrame(
A = [1, 2, 3, 4, 1],
B= ["M", "F", "F", "M", "O"],
)
df2 = DataFrame(
AA = ["aa", "bb"],
AB = [10.1, 10.2],
)
filepath = "./xlsxtest.xlsx"
startrow = nrow(df1) + 3
startcolumn = 1
XLSX.openxlsx(filepath, mode="w") do file
sheet = file[1]
XLSX.writetable!(sheet, df1)
XLSX.writetable!(sheet, df2, anchor_cell=XLSX.CellRef(startrow, startcolumn)) # or XLSX.CellRef("A8")
end
Additional options are shown in the documentation here.