Tag: stringio

将Excel(xlsx)文件提供给用户在Django(Python)中下载

我正在尝试使用Django创build和提供excel文件。 我有一个jar文件获取参数,并根据参数生成一个excel文件,它没有问题。 但是,当我试图获取生成的文件,并将其提供给用户下载文件出来破碎。 它有0kb的大小。 这是我用于Excel生成和服务的代码片断。 def generateExcel(request,id): if os.path.exists('./%s_Report.xlsx' % id): excel = open("%s_Report.xlsx" % id, "r") output = StringIO.StringIO(excel.read()) out_content = output.getvalue() output.close() response = HttpResponse(out_content,content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename=%s_Report.xlsx' % id return response else: args = ['ServerExcel.jar', id] result = jarWrapper(*args) # this creates the excel file with no problem if result: excel […]

使用Pandas Excelwriter写入到StringIO对象?

我可以传递一个StringIO对象到pd.to_csv()就好了: io = StringIO.StringIO() pd.DataFrame().to_csv(io) 但是在使用excel作者的时候,我遇到了很多麻烦。 io = StringIO.StringIO() writer = pd.ExcelWriter(io) pd.DataFrame().to_excel(writer,"sheet name") writer.save() 返回一个 AttributeError: StringIO instance has no attribute 'rfind' 我正在尝试创build一个ExcelWriter对象,而不调用pd.ExcelWriter()但我有一些麻烦。 这是我迄今为止所尝试的: from xlsxwriter.workbook import Workbook writer = Workbook(io) pd.DataFrame().to_excel(writer,"sheet name") writer.save() 但是现在我得到一个AttributeError: 'Workbook' object has no attribute 'write_cells' 如何将excel格式的pandas数据StringIO保存到StringIO对象?