Exporting AgGrid to Excel

Hi @PablocFonseca Thank you for providing a great AgGrid component. I need to export AgGrid component (not pandas dataframe!) to Excel worksheet by clicking an st.button (not right-click on the component). I know that JS Grid implementation has gridOptions.api.exportDataAsExcel() function - but I need help on how to use it from Streamlit/Python.
Appreciate the suggestions from the other community members.

Hi, have you gotten the solution to this? I also need to export data to excel

There are a number of posts on the forum about download dataframes in excel format. Combining that with aggrid might look something like this:

import io

import pandas as pd
import st_aggrid
import streamlit as st

df = pd.DataFrame(
    {
        "col1": [1, 2],
        "col2": [3, 4],
    }
)


# Add aggrid table to display dataframe
output = st_aggrid.AgGrid(
    df,
    editable=True,
)

out_df = output["data"]

st.write(out_df)


def to_excel(df) -> bytes:
    output = io.BytesIO()
    writer = pd.ExcelWriter(output, engine="xlsxwriter")
    df.to_excel(writer, sheet_name="Sheet1")
    writer.save()
    processed_data = output.getvalue()
    return processed_data


st.download_button(
    "Download as excel",
    data=to_excel(out_df),
    file_name="output.xlsx",
    mime="application/vnd.ms-excel",
)
1 Like

Thank you!!