# Create excel file using data from two dataframes

**URL:** <https://discourse.julialang.org/t/create-excel-file-using-data-from-two-dataframes/110320>\
**Category:** General Usage\
**Tags:** dataframes, xlsx, excel\
**Created:** [February 16, 2024, 9:47pm UTC](https://discourse.julialang.org/t/create-excel-file-using-data-from-two-dataframes/110320 "2024-02-16T21:47:01Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 19, 2024, 3:03pm UTC](https://discourse.julialang.org/t/create-excel-file-using-data-from-two-dataframes/110320/6 "2024-02-19T15:03:45Z")

</div>

The `"rw"` mode is a little dangerous, especially if your Excel document contains formulas ([reference](https://github.com/felipenoris/XLSX.jl/issues/159)). Also, the `do` block form is more robust; it will always automatically close the file.

This is how I would do it.

```julia
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](https://felipenoris.github.io/XLSX.jl/dev/tutorial/#Writing-Excel-Files).

---

_[View the full topic](https://discourse.julialang.org/t/create-excel-file-using-data-from-two-dataframes/110320)._
