# Writing a dataframe to Excel XLSX

**URL:** <https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748>\
**Category:** Data\
**Tags:** dataframes, excel\
**Created:** [April 21, 2021, 6:16pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748 "2021-04-21T18:16:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [April 21, 2021, 6:16pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/1 "2021-04-21T18:16:35Z")

</div>

Can we write a dataframe (without headers) to a specified sheet and specified cell in Excel? I have a dataframe that I want to write to a specific sheet in a spreadsheet. Consider this dataframe

```julia
df = DataFrame(Name=["aaaa","bbbb","cccc"], Type = [1,2.0,5.5])

```

I have a spreadsheet “MyXLfile.xlsx” which has a tab called “Required\_tab”, and I want to write this dataframe df (without headers) to the cell starting A1 in this tab.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 21, 2021, 7:11pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/2 "2021-04-21T19:11:30Z")

</div>

Have you seen XLSX.jl? I don’t know all of its features, but it would be a good place to start.

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [April 21, 2021, 7:34pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/3 "2021-04-21T19:34:48Z")

</div>

Yes, I have gone through documentation of XLSX. The examples given in documentation either write single values to cells or just write a dataframe by creating a new Excel file:  
[https://felipenoris.github.io/XLSX.jl/stable/tutorial/#Writing-Excel-Files](https://felipenoris.github.io/XLSX.jl/stable/tutorial/#Writing-Excel-Files)  
I have tried the code below but it gives an error message

```julia
XLSX.openxlsx("MyXLfile.xlsx", mode="rw") do xf
    sheet = xf["Required_tab"]
    sheet["A1"] = XLSX.writetable(sheet, collect(DataFrames.eachcol(df1)), DataFrames.names(df1))
end

```

The code below also doesn’t work

```julia
XLSX.openxlsx("MyXLfile.xlsx", mode="rw") do xf
    sheet = xf["Required_tab"]
    sheet["A1"] = df1
end

```

Maybe @felipenoris can point me in the right direction

---

<div class="post-metadata">

**Author:** ![SymKo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/symko/32/17753_2.png) [@SymKo](https://discourse.julialang.org/u/SymKo)\
**Post date:** [April 21, 2021, 7:38pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/4 "2021-04-21T19:38:22Z")

</div>

You try something like this

```julia
XLSX.openxlsx("my_new_file.xlsx", mode="rw") do xf
    sheet=xf["Required_tab"]
    sheet["A1"]= df.Type
end

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 21, 2021, 7:44pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/5 "2021-04-21T19:44:07Z")

</div>

You can write the data and add a new tab by

```julia
using DataFrames
using XLSX
df = DataFrame(Name=["aaaa","bbbb","cccc"], Type = [1,2.0,5.5])
XLSX.openxlsx("MyXLfile.xlsx",mode="rw") do xf
     XLSX.addsheet!(xf,"Required_tab")
     sheet = xf["Required_tab"]
     for r in 1:size(df,1), c in 1:size(df,2)
          sheet[XLSX.CellRef(r , c )] = df[r,c]
     end
end

```

---

<div class="post-metadata">

**Author:** ![Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith/32/13567_2.png) [@Smith](https://discourse.julialang.org/u/Smith)\
**Post date:** [April 21, 2021, 9:32pm UTC](https://discourse.julialang.org/t/writing-a-dataframe-to-excel-xlsx/59748/6 "2021-04-21T21:32:35Z")

</div>

Thank you.
