You can use Containers.rowtable
for this. See Containers · JuMP.
julia> using JuMP
julia> import CSV
julia> import HiGHS
julia> model = Model(HiGHS.Optimizer);
julia> set_silent(model)
julia> @variable(model, x[i=1:5, j=2:3] >= i + j);
julia> @objective(model, Min, sum(x));
julia> optimize!(model)
julia> CSV.write(
"output.csv",
Containers.rowtable(value, x; header = [:i, :j, :value]),
)
"output.csv"
shell> cat output.csv
i,j,value
1,2,3.0
2,2,4.0
3,2,5.0
4,2,6.0
5,2,7.0
1,3,4.0
2,3,5.0
3,3,6.0
4,3,7.0
5,3,8.0
julia> import XLSX
julia> import DataFrames
julia> table = Containers.rowtable(value, x; header = [:i, :j, :value])
10-element Vector{NamedTuple{(:i, :j, :value), Tuple{Int64, Int64, Float64}}}:
(i = 1, j = 2, value = 3.0)
(i = 2, j = 2, value = 4.0)
(i = 3, j = 2, value = 5.0)
(i = 4, j = 2, value = 6.0)
(i = 5, j = 2, value = 7.0)
(i = 1, j = 3, value = 4.0)
(i = 2, j = 3, value = 5.0)
(i = 3, j = 3, value = 6.0)
(i = 4, j = 3, value = 7.0)
(i = 5, j = 3, value = 8.0)
julia> XLSX.writetable("output.xlsx", "output" => DataFrames.DataFrame(table))