XLSX.jl: writing sheets from vectors

In the package XLSX.jl, there is a useful capability of writing multiple excel sheets into a new file from several dataframes. From the documentation:

XLSX.writetable("report.xlsx", REPORT_A=( collect(DataFrames.eachcol(df1)), DataFrames.names(df1) ), REPORT_B=( collect(DataFrames.eachcol(df2)), DataFrames.names(df2) ))

If I have two vectors, one of sheet titles (i.e., report_vec = [REPORT_A, REPORT_B, …]) , the other vector of DataFrames (i.e., df_vec = [df1, df2, …]), each of length about 20, is it possible to write them all into the same excel file?

It feels as though I’m missing something very simple that involves splatting, but I just can’t get it. Any help very much appreciated.

See below. It ends up a little complicated, building the NamedTuple, then splatting it into writetable

using DataFrames
import XLSX

report_vec = ["REPORT A", "REPORT B"]
dfs = [DataFrame(a=[1, 2, 3]), DataFrame(b=[4, 5, 6])]

nt = (;(Symbol(name) => (collect(eachcol(df)), names(df)) for (name, df) in zip(report_vec, dfs))...)
XLSX.writetable("test.xlsx"; nt...)
2 Likes