【问题标题】:How to show an excel file in Django webpage如何在 Django 网页中显示 excel 文件
【发布时间】:2019-05-15 12:26:35
【问题描述】:

我是 Django 新手,我有一个 Django 应用程序,一旦您上传了几个 excel 文件,它就会进行后台计算并将结果保存在一个 excel 文件中。我需要在 Django 主页中将那些处理最少的 excel 文件显示为数据表。我尝试使用 iframe 的 django-excel 包来执行此操作。我无法获取该页面。请提出以下代码是否需要修改。有没有其他方法可以解决这个问题?

在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)

在 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>

当单击“showCombined”按钮时,我希望在主页中显示生成的 excel 文件

【问题讨论】:

    标签: python django django-excel


    【解决方案1】:

    有多种选择,具体取决于您是否希望无法使用 excel 的用户可以访问电子表格数据。

    最常用的按钮是一个超链接,它调用一个 Django 视图,该视图将电子表格数据呈现为 HTML &lt;table&gt;。有一个非常棒的 JavaScript,称为 DataTables,它可以使这样的表格在显示方面几乎与 Excel 一样功能。非常易于使用:从简单开始,然后添加调整直到完善。绝对有很多可用的示例(并且在 StackOverflow 上提供了很多支持)。

    另一个是用于将其结果保存为 Excel 的计算,同时也保存为 pdf,并在该超链接中提供 pdf。

    最后,您可以将链接的目标设置为 xlsx 文件本身,如果 Excel 尚未设置为默认设置(在 Windows 机器上可能会设置为默认设置,则用户的浏览器将询问如何打开它。打开Linux 盒子 LibreOffice Calc 可能会完成同样的工作。)。

    【讨论】:

    • 感谢@nigel222 的快速回答。您是否有任何工作示例或使用调用 excel 作为 html 表的简短演示。由于我是 Web 开发的新手,所以我对此没有太多了解。感谢您的帮助。
    • 对不起,没有。如果您可以访问后台应用程序,只需将结果保存为csv 文件和xls[x](或自定义数据文件,或 JSON)。 Django 可以快速访问csv 文件,并通过模板将数据渲染成html 表格。 (查找 Python csv 模块,如果是 JSON,则查找 json)。如果不能,您将不得不从 Excel 文件中提取数据,而我完全没有这样做的经验。数据表的文档非常精美。
    猜你喜欢
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2023-03-21
    • 2023-02-19
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多