I have four data frames (D1, …, D4) usually having fixed number of columns, but varying numer of rows. I would like to write them next to each other to a single new sheet in an existing xlsx file. I use the packages XLSX and DataFrames. The excel-file is in the working directory, like my program.
I use the following function in the function:
function export_to_xlsx_sheet(XF::AbstractString, sheet_name::AbstractString, D1::DataFrame, D2::DataFrame, D3::DataFrame, D4::DataFrame)
# Load existing Excel file
xf = XLSX.readxlsx(XF)
# Create a new worksheet for the dataframes to file XF
XLSX.addsheet!(xf, sheet_name)
# Write dataframes to the new sheet
XLSX.writetable!(sheet_name, D1, anchor=“A1”)
XLSX.writetable!(sheet_name, D2, anchor=size(D1, 1) + 2)
XLSX.writetable!(sheet_name, D3, anchor=size(D1, 1) + size(D2, 1) + 4)
XLSX.writetable!(sheet_name, D4, anchor=size(D1, 1) + size(D2, 1) + size(D3, 1) + 6)
# Save changes to the Excel file
XLSX.writexlsx(XF, xf)
end
I am probably doing something wrong. I get the message: “XLSXFile instance is not writable”. Can anyone help to get the dataframes to the single new sheet in the exisitng file?
Hi rocco,
This certainly is a move in the right direction. A sheet is added now to the output file. The data frames are now vertically stacked, whereas I would like them horizontally added, but that is probably rearranging the cell references. I’ll try to elaborate that!
The anchor_cell keyword argument must be passed a XLSX.CellRef. Rocco showed how to use a cell name for your anchor cell, but you can also use row and column indices as I did in the link.
Hi rocco,
Thanks this works well. Since the number of columns per data frame is fixed, I could do the following:
XLSX.openxlsx(XF, mode=“rw”) do xf
newsheet=XLSX.addsheet!(xf, sheet_name)
XLSX.writetable!(newsheet, D1, anchor_cell=XLSX.CellRef(“A1”))
XLSX.writetable!(newsheet, D2, anchor_cell=XLSX.CellRef(“B1”))
XLSX.writetable!(newsheet, D3, anchor_cell=XLSX.CellRef(“C1”))
XLSX.writetable!(newsheet, D4, anchor_cell=XLSX.CellRef(“F1”))
end
This looks simple. I am still a bit puzzled why it was not writable. But I am fairly new to Julia, so I guess it is probably part of the learning curve. Thanks a lot !
Thanks, Nathan and Rocc, I get your point. Calculations to determine cell refs are possible. Great. Now, I also understand why the file was not writeable. Thanks for adding to my understanding.
Wanting to write the second table next to the first and not below (as you specified), it was necessary to calculate the anchor_cell.
To make it clearer, in the example the first dataframe, placed at “A1”, has 4 columns, so we want to paste the second starting from the fifth column i.e. from cell “E1”.
To do this, in general terms, I use the formula col_n=('A'+size(D1, 2))*'1'
Below I break it down into the various pieces to make the content clearer.
know :
that in Julia the function is defined that adds an integer to a character to obtain the character that is n positions later in the asci table.
the function *(str1,str2) is also defined or in the infixed form str1 * str2 which concatenates multiple strings and/or characters.
So col_n=('A'+size(D1, 2))*'1' simply attaches the ‘1’ character to the end of the string.
Wow, I wasn’t aware yet, that an integer can be added to a character, i.e. its ASCII/Unicode. But this makes manipulation of characters very convenient.I see the *‘1’ concatenates the column character and the row number to make the reference cell. Thanks for the help.