Reputation: 35
I have variables that stored in julia notebook called
sheet1 = [1 2 3; 4 5 6]
and sheet2 = [1 1 1; 2 2 2]
how do i write this variable into excel workbook with a different worksheet?
and how if i want in a same worksheet?
xlxs.addsheet!
didnt work , tq
Upvotes: 1
Views: 673
Reputation: 4370
Modifying an example from the documentation,
using XLSX
filename = "myfile.xlsx"
# Some example data to try writing to .xlsx
columns = Vector()
push!(columns, [1, 2, 3])
push!(columns, ["a", "b", "c"])
labels = [ "column_1", "column_2"]
XLSX.openxlsx(filename, mode="w") do xf
sheet = xf[1]
# Write our data to sheet 1
XLSX.writetable!(sheet, columns, labels, anchor_cell=XLSX.CellRef("A1"))
# Write the same data, but to a different place in the sheet
XLSX.writetable!(sheet, columns, labels, anchor_cell=XLSX.CellRef("D1"))
# Add a new sheet, which we will then access with xf[2]
XLSX.addsheet!(xf)
# Write the same data, but to sheet 2, in yet another position
XLSX.writetable!(xf[2], columns, labels, anchor_cell=XLSX.CellRef("B2"))
end
You were on the right track with XLSX.addsheet!
, but have to then write to that second sheet, the reference to which is stored in the second position of xf
in this example. This example also shows how to write to different positions within the same sheet, using the anchor_cell
keyword option.
Upvotes: 2
Reputation: 42214
If you prefer a one liner this can be done as:
XLSX.writetable("file.xlsx",
sheet1=(collect(eachcol(sheet1)), 'a':'c'),
sheet2=(collect(eachcol(sheet2)), string.("col", 1:3)) )
Upvotes: 3