Jmcnamara向您指出了正确的方向。翻译成您的问题,您正在寻找以下代码:
sio = StringIO()PandasDataframe = pandas.Dataframe(self.csvdict)PandasWriter = pandas.ExcelWriter(sio, engine='xlsxwriter')PandasDataframe.to_excel(PandasWriter, sheet_name=sheetname)PandasWriter.save()sio.seek(0)workbook = sio.getvalue()response = StreamingHttpResponse(workbook, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')response['Content-Disposition'] = 'attachment; filename=%s' % filename
请注意,您实际上是将数据保存到StringIO变量而不是文件位置。这样,您可以防止在生成响应之前保存文件。



