我有一个应用程序,它将在单击按钮时下载一个excel文件。我有一个FileField存储在数据库中,我想在该.xlsx文件中插入它,使其显示为图标。
我已经尝试过使用xlsxwriter和openpyxl库,但我认为它们不能插入对象。如果您知道该工作的任何其他库,那会有所帮助。
class SearchView(View):
def WriteToExcel(self,item):
output = io.BytesIO()
**workbook = xlsxwriter.Workbook(output)**;
worksheet_s=workbook.add_worksheet("Reports")
header = workbook.add_format({
#some format
})
worksheet_s.write('A1', "Date",header)
#Some Headings
worksheet_s.write('E1', "Attachment",header)
row = 1
for data in item:
worksheet_s.write(row, 0, data.date)
#Write stuff
**worksheet_s.write(row, 4,data.document)**#Here I want the file to be inserted as an icon
row += 1
workbook.close()
xlsx_data = output.getvalue()
return xlsx_data
def post(self,request):
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=Report.xlsx'
xlsx_data = self.WriteToExcel(item)
response.write(xlsx_data)
return response