Andreas Dibiasi
Andreas Dibiasi

Reputation: 271

Export Arrays to Excel Sheets in Julia

In Julia, is it possible to export arrays into separate excel sheets. Let's say I have the following two arrays

A = ones(4,4)
B = ones(5,100)

and I want to save Array A in Sheet A and Array B in Sheet B of the same Excel file.

Upvotes: 2

Views: 992

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

Here it is:

using Pkg
Pkg.add("XLSX")

using XLSX
A = reshape(1:20,4,5)
B = reshape(51:100,10,5)

function fill_sheet(sheet, arr)
    for ind in CartesianIndices(arr)
        XLSX.setdata!(sheet, XLSX.CellRef(ind[1], ind[2]), arr[ind])
    end
end

XLSX.openxlsx("sample2.xlsx", mode="w") do xf
    s  = XLSX.addsheet!(xf,"SheetName_A")
    fill_sheet(s,A)
    s = XLSX.addsheet!(xf,"SheetName_B")
    fill_sheet(s,B)
end

Upvotes: 2

Related Questions