Pierre-Sassoulas / django-survey

A django survey app that can export results as CSV or PDF using your native language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any plan to export excel?

PaleNeutron opened this issue · comments

Excel is much more popular in company, especially for users who are not a developer.

I am building an web app for my co-workers, and they want to export survey result in excel format for daily usage.

Hello, there is already a CSV export. The file can be automatically opened in excel with a little trick. IE. adding a line containing "sep=," at the top of the file. Replacing csv = [] by csv = ['"sep=,"'] here : https://github.com/Pierre-Sassoulas/django-survey/blob/master/survey/exporter/csv/survey2csv.py#L72 should do the trick (2mn if you know where the lib is on your server). I won't merge this change, because it breaks the CSV export for non-excel users.

A change I could merge though would be a "CSV for Excel" exporter available at another URL than the CSV export or switchable between Excel or not in settings (Probably 1h of work). We could also create an XLSX exporter, relatively easily (2/6 hours). Adding graphics automatically for each questions like for the PDF exporter would take some time (1 to 2 weeks ?).

Please feel free to make a pull request, those change would be welcome. Being able to import questions from excel would probably be nice too.

Adding an option using the hack seemed fast to do, so I did. Can you try to add EXCEL_COMPATIBLE_CSV = True in your setting file after updating to 1.3.17 ?

Thanks, that works well.

BTW, be aware, excel's default csv encoding is `UTF8-BOM.

excel's default csv encoding is UTF8-BOM

Is it causing a problem for opening the exported CSV? (You have to choose the encoding?)

Yes, if csv file contains any non-unicode string and not encoded by UTF8-BOM, excel can not decode them correctly.

Further more, I write an action to export csv in admin site. I thought it is easier to use than command line.

import csv
import codecs
from django.http import HttpResponse

...

    @staticmethod
    def export_as_csv(self, request, queryset):
        survey = queryset.first()

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
        # BOM
        response.write(codecs.BOM_UTF8)
        writer = csv.writer(response, delimiter=',')

        header, question_order = Survey2Csv.get_header_and_order(survey)
        writer.writerow(header)
        for rsp in survey.responses.all():
            line = Survey2Csv.get_user_line(question_order, rsp)
            writer.writerow(line)
        return response

here is my implement.

I think it could be better by...

  1. I just export the first query selected. It could be exporting all and zipped in a zip/tar.gz file.
  2. I found during export, you query the database once per response, it may be a risk if we have thousands of response.

Ok, this is a bug then, I'm going to open a ticket for the BOM_UTF8 encoding.

For the admin actions, I did not know this existed. It's a really neat idea. It could also be done for PDF report.

Regarding performance (a query for each question), unless you have a quick solution to make it efficient I would worry about it when someone consider that the CSV generation time is too long. But I do welcome PR for this if you know a way to make it better.

Could you open a merge request with your changes, please?