Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

mysql - Exporting issues to excel using openpyxl(django) (cant seems to work with fetchall())

def export_as_xls(self, request, queryset):
    opts = self.model._meta
    file_name = unidecode(opts.verbose_name)
    sql_query = '''SELECT  
                    COUNT(id) AS No_Of_Report, 
                    vendor, 
                    country_code, 
                    SUM(new_code)*100/SUM(sent) AS 'failure_rate', 
                    SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate' 
                    FROM sms_statistics 
                    WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
                    GROUP BY vendor, country_code 
                    ORDER BY vendor DESC;'''

This is mysql query i used to call for the data in mysql schema

    field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
    wb = Workbook()
    ws = wb.active
    ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
    with connection.cursor() as cursor:
        cursor.execute(sql_query)
        objects = list(cursor.fetchall())
        for row in cursor.fetchall():
            objects = list(row)
            ws.append(objects)
            print(ws.append(row))
    ws = style_output_file(ws)

I think the issue is right here for not being able to export to excel. Im not be using the right method to export the file from action.py

response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx' wb.save(response) return response export_as_xls.short_description = "Reports of Past 30 days" export_as_xls.acts_on_all = True

Blockquote

I believe this part is fine as i tried exporting an empty file and its working as expexted

question from:https://stackoverflow.com/questions/66058855/exporting-issues-to-excel-using-openpyxldjango-cant-seems-to-work-with-fetcha

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
def export_thirty_days(self, request, queryset):
    opts = self.model._meta
    file_name = "Sms Statistic Report"
    sql_query = '''SELECT  
                    COUNT(id) AS No_Of_Report, 
                    vendor, 
                    country_code, 
                    SUM(new_code)*100/SUM(sent) AS 'failure_rate', 
                    SUM(case when new_code =0 then 1 ELSE 0 end)*100/sum(sent) AS 'success_rate' 
                    FROM sms_statistics 
                    WHERE FROM_UNIXTIME(date) >= curdate() - interval 30 day
                    GROUP BY vendor, country_code 
                    ORDER BY COUNT(id) DESC;'''
    field_names = ('No of report', 'Vendor', 'Country Code', 'Failure Rate', 'Success Rate')
    wb = Workbook()
    ws = wb.active
    ws.append(ExportExcelAction.generate_header(self, self.model, field_names))
    with connection.cursor() as cursor:
        cursor.execute(sql_query)
        for row in cursor.fetchall():
            l = list(row)
            ws.append(l)
    ws = style_output_file(ws)
    response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = f'attachment; filename={file_name}.xlsx'
    wb.save(response)
    return response
export_thirty_days.short_description = "Reports of Past 30 days"
export_thirty_days.acts_on_all = True

This is the solutions i've found to make it work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...