1

I am new to Django and I have a Django application where once you upload few excel files, it does a background calculation and saves the results in an excel file. I need to show those excel files with minimal processing in the Django home page as datatable. I tried django-excel package with Iframe to do so. I am not not able to get the page. Please suggest if the code below needs to be modified. Is there any other ways to solve this problem?

In views.py:

import django_excel as excel
import pyexcel as p
def show_result_files(request,foldername,outputfilenames):
    file_path = path + '/Data/' + foldername
    if os.path.isfile(file_path + 'Final_Results.xlsx'):
        for afile in request.FILES.getlist(outputfilenames):
            combined_sheet = p.get_sheet(file_path + '2020MY EWPR Combined Parts List Final.xlsx')
            combined_sheet.save_as(afile.sortable.html)
            from IPython.display import IFrame
            IFrame("afile.sortable.html", width=600, height=500)

In home.html:

<div class="inline col-md-9" id="showFinalResults">
    <form method="POST" name="showCombined" action="">{% csrf_token %}
        <button name="showCombined" type="submit" class="btn btn-primary">Combined Parts List</button>
    </form>
</div>
<div class="inline col-md-9" id="showFinalResults">
    <form method="POST" name="showCombined" enctype="multipart/form-data" action=''>{% csrf_token %}
        <div class="col">
            <div class="row">
                <span class="btn btn-primary btn-file">
                                        Combined Parts List <input name="SparrowFiles" id="Sparrow" type="file" multiple="">
                </span>
                                    <button name='upload_Sparrow' type="submit" class="btn btn-secondary js-upload-files" data-toggle="modal" data-target="#modal-progress_sp"">Go</button>
            </div>
        </div>
    </form>
</div>

What I want the resulting excel file displayed in the Home page when a "showCombined" button is clicked

Shantanu Sharma
  • 666
  • 6
  • 21
neel1923
  • 21
  • 1
  • 2

1 Answers1

3

There are various options, depending on whether you want the spreadsheet data to be accessiblew to users who cannot use excel, or not.

The most general is for the button to be a hyperlink that invokes a Django view which renders the spreadsheet data as an HTML <table>. There is a completely awesome JavaScript called DataTables that can make such a table almost as functional as Excel insofar as display is concerned. Very easy to use: start simple, then add tweaks until perfected. Absolutely loads of examples available (and much support with it on StackOverflow).

Another is for the computation which saves its results as Excel to also save as pdf alongside, and to serve up the pdf in that hyperlink.

Finally, you can make the target of the link the xlsx file itself, and the user's browser will ask how to open it if Excel is not already set up as default (which it probably will be, on a Windows box. On a Linux box LibreOffice Calc will probably accomplish much the same.).

nigel222
  • 7,582
  • 1
  • 14
  • 22
  • Thanks @nigel222 for your quick answer. Do you have any working example or a short demonstration of use of invoking excel as html table. As I am new to web development, I dont have much idea about it. Appreciate your help. – neel1923 May 15 '19 at 13:50
  • Sorry, no. If you can get at the background application, just make it save the results as a `csv` file as well as `xls[x]` (or as a custom data file, or as JSON). Django can quickly access a `csv` file and render the data through a template into an html table. (Look up the Python `csv` module, or `json` if JSON). If you can't, you are going to have to extract the data from an Excel file, and I have no experience at all of doing that. Datatables is quite beautifully documented. – nigel222 May 16 '19 at 18:35