Hello,

I have been trying to export filtered table data to excel but was unlucky.

I can filter the table by date range successfully but can export the
filtered data.

Below is my view.py. Please help....

def export_sickoff_export(request):
    if request.method=="POST":
        start_date = request.POST.get('start_date')
        end_date = request.POST.get('end_date')
        search_results =
Prescription.objects.filter(date_precribed__range=[start_date,end_date])

        return render(request,
'pharmacist_templates/reports/referrals_report.html', {"data":
search_results})

        response = HttpResponse(content_type='application/ms-excel')
        response['Content-Disposition'] = 'attachment;
filename=Referrals & Sick-offs' + \
            str(datetime.datetime.now())+'.csv'

        wb = xlwt.Workbook(encoding='utf-8')
        ws = wb.add_sheet('Prescription,Patients')

        #Sheet header, first row
        row_num = 0

        font_style = xlwt.XFStyle()
        font_style.font.bold = True

        columns = ['Payroll No', 'First Name', 'Last Name', 'Non Work
Related SickOff', 'Work Related SickOff', 'Referral', 'Remarks',
'Date']

        for col_num in range(len(columns)):
            ws.write(row_num, col_num, columns[col_num], font_style)

        # Sheet body, remaining rows
        font_style = xlwt.XFStyle()

        rows = 
Prescription.objects.filter(nurse=request.user.pharmacist).values_list('patient_id__reg_no',
'patient_id__first_name', 'patient_id__last_name',
'non_work_related_sickOff', 'work_related_sickOff', 'referral',
'remarks', 'date_precribed')

        for row in rows:
            row_num += 1
            for col_num in range(len(row)):
                ws.write(row_num, col_num, str(row[col_num]), font_style)
                # for patient in patients:
                #     writer.writerow(patient)

        wb.save(response)
        return response

    else:
         displaydata =
Prescription.objects.filter(nurse=request.user.pharmacist).order_by('-id')
         return render(request,
'pharmacist_templates/reports/referrals_report.html',
{"data":displaydata})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADYG20H0kkewMMV4s_Uxbm%2BwS1HYQGtpfRDfWM%3DUMXj%3DeQiKyw%40mail.gmail.com.

Reply via email to