# Write to XLSX file while maintaining formatting

**URL:** https://discourse.julialang.org/t/write-to-xlsx-file-while-maintaining-formatting/87715
**Category:** General Usage
**Tags:** question, xlsx, formatting
**Created:** [September 23, 2022, 6:40pm UTC](https://discourse.julialang.org/t/write-to-xlsx-file-while-maintaining-formatting/87715 "2022-09-23T18:40:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Carl\_Talsma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carl_talsma/32/33747_2.png) [@Carl\_Talsma](https://discourse.julialang.org/u/Carl_Talsma)
#### Post date: [September 23, 2022, 6:40pm UTC](https://discourse.julialang.org/t/write-to-xlsx-file-while-maintaining-formatting/87715/1 "2022-09-23T18:40:46Z")

</div>

Hi,

I’m trying to write data to a .xlsx file that has already been formatted (cell size, colors, etc.). Is there anyway to write data to a .xlsx file while maintaining the existing formatting? I’m currently using the XLSX.jl function writetable!() shown in the code below. However, this does not maintain the formatting of the original file.

```julia
    XLSX.openxlsx(filename, mode="rw") do xf
        sheet = xf[1]
        XLSX.writetable!(sheet, collect(DataFrames.eachcol(df)), DataFrames.names(df))
    end

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 24, 2022, 8:23am UTC](https://discourse.julialang.org/t/write-to-xlsx-file-while-maintaining-formatting/87715/2 "2022-09-24T08:23:07Z")

</div>

Linking [related thread](https://discourse.julialang.org/t/merging-and-formatting-cells-in-excel-from-julia/18786/2) with an alternative solution.

---

<div class="post-metadata">

### Author: ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)
#### Post date: [August 30, 2026, 2:54pm UTC](https://discourse.julialang.org/t/write-to-xlsx-file-while-maintaining-formatting/87715/3 "2026-08-30T14:54:34Z")

</div>

This works now in the main branch of XLSX.jl and will be released in the next release. An incomplete fix was made in v0.11 (issue [#281](https://github.com/JuliaData/XLSX.jl/issues/281)). This was completed in response to issue [#454](https://github.com/JuliaData/XLSX.jl/issues/454) and will be in v0.12.4.

To illustrate:

```julia-auto
using XLSX

# Create a new, empty xlsx file with hundred empty cells 
# and then format the empty cells
openxlsx("formatted.xlsx"; mode="w") do xf
    sh = xf[1]
    sh[1:10, 1:10] = ""
    setAlignment(sh, "A1:J1"; wrapText=true, horizontal="center")
    setRowHeight(sh, 1, 1:10; height=32)
    setFormat(sh, 2:10, [1,5,9]; format="#,##0.000_0")
    setFormat(sh, 2:10, [2,6,10]; format="_0£* #,##0_0")
    setFormat(sh, 2:10, [3,4,7,8]; format="0.0_0")
end

data = [10000 .* rand(9) for _ in 1:10] # generate some random data
titles = ["Title for Column $(i)" for i in 1:10] # and column names

# Add the data to the preformatted worksheet
xf = openxlsx("formatted.xlsx", mode="rw")
sh=xf[1]
writetable!(sh, data, titles)
writexlsx("filled_and_formatted.xlsx", xf; overwrite=true)

```

The formatting is preserved through the `writetable!`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d7159c4da3cf89c73c4a43d511c8ad42dd6eeaa.png)

It has worked unconditionally for cell font, fill, borders and alignment since v0.11
