How to write matrix data to Excel in separate worksheets?

I have matrix data type that stored in atom workspace 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?

xlxs.addsheet! didnt work , tq

Which package are you using? Your last line suggests you might be using XLSX, have you looked at the relevant docs, which seem to cover what you’re after?

Yes i’m using XLSX , but the problem is i want to export multiple matrix to a single excel file. Each matrix will be put in seperate worksheet.

I already read about the documentation, but still didnt get the answer

@adikaagas, welcome to discourse. Recommend posting a MWE as per guidelines.

NB: edited code to cover one additional use case

using XLSX

data1 = [[1, 2, 3], [4, 5, 6]];
data2 = [[1, 1, 1], [2, 2, 2]];    
data3 = [[0, 1,-1], [-2,0, 2]];

# write data1 to SHEET_A and data2 to SHEET_B
XLSX.writetable("TEST123.xlsx", SHEET_A=(data1, [:col1,:col2]),
       SHEET_B=(data2, [:col1 :col2]), overwrite=true)

# write data3 into existing SHEET_A
XLSX.openxlsx("TEST123.xlsx", mode="rw") do xf
    sheet = xf[1]
    XLSX.writetable!(sheet, data3, [:col1,:col2], anchor_cell=XLSX.CellRef("B10"))

# write data3 to new SHEET_C
XLSX.openxlsx("TEST123.xlsx", mode="rw") do xf
    n = XLSX.sheetcount(xf)
    XLSX.addsheet!(xf, "SHEET_C")
    sheet = xf[n+1]
    XLSX.writetable!(sheet, data3, [:col1,:col2])

Thankyou for the answer, it helps a lot to undertand about XLXS documentation

1 Like