title: Write data to a CSV on the fly and download the file using fast api
body: <p>I am getting corrupted data when I am downloading the file using the api:</p>
  <pre><code>file_name = 'download' 
  prepared_df = pd.DataFrame.from_dict(data)
  path_to_excel = os.path.join(dir_to_download, file_name)
  writer = pd.ExcelWriter(path_to_excel, engine='xlsxwriter')
  prepared_df.to_excel(writer, sheet_name=&quot;Sheet1&quot;, index=False)
  writer.save()
  writer.close()
  </code></pre>
  <p>Downloading file using <strong><code>FileResponse</code></strong>:</p>
  <pre><code>FileResponse(path=path_to_excel, filename=file_name, media_type='text/xlsx')
  </code></pre>