Download button for csv or xlsx file


I currently struggling with the use of the streamlit.download_button .

  1. I’m uploading a xlsx file to the streamlit app via file_uploader
  2. Read the excel file with pd.read_excel to data_source object.
  3. Write the dataframe object with st.write. The dataframe displays correctly
  4. Download the file with st.download_button with the following code :

st.download_button(‘Download file’,data=pd.DataFrame.to_csv(data_source,index=False), mime=“text/csv”)

Results = I get a csv file but with encoding problems. When I open the file I get the following :

Allée van

When I don’t specify a mime argument. it download as a .txt file and doesn’t give those encoding errors


Allée van

I tried to export as a xlsx file also (which was the initual target) but it’s giving me a error message when I open the file so I swithced to csv.

Could you help ?



Hi Simon
I was struggled with the same problem before, you can have a shot with following code which works fine with me.

import pandas as pd
from io import BytesIO
from pyxlsb import open_workbook as open_xlsb
import streamlit as st

def to_excel(df):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, index=False, sheet_name='Sheet1')
    workbook =
    worksheet = writer.sheets['Sheet1']
    format1 = workbook.add_format({'num_format': '0.00'}) 
    worksheet.set_column('A:A', None, format1)
    processed_data = output.getvalue()
    return processed_data
df_xlsx = to_excel(df)
st.download_button(label='📥 Download Current Result',
                                data=df_xlsx ,
                                file_name= 'df_test.xlsx')

Check this out as well: Download Button with Excel File - #7 by snehankekre