当前位置:   article > 正文

【Django】 读取excel文件并在前端以网页形式显示-安装使用Pandas_django表格显示前端

django表格显示前端

安装pandas

Pandas是一个基于NumPy的Python数据分析库,可以从各种文件格式如CSV、JSON、SQL、Excel等导入数据,并支持多种数据运算操作,如归并、再成形、选择等。

  • 更换pip源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  • 1
  • 安装Pandas
pip install pandas
  • 1

在这里插入图片描述

写views

视图:

from django.shortcuts import render
from django.http import HttpResponse
from antproject.settings import BASE_DIR
import pandas as pd

# Create your views here.
def hello(request):
    return HttpResponse("this is hello/")

def show_excel(request):
    df=pd.read_excel(BASE_DIR / "data/score.xlsx")
    cont="""
        <table>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>语文</th>
                <th>数学</th>
                <th>英语</th>
            </tr>
    """ 
    for idx, row in df.iterrows():
        cont += f"""
            <tr>
                <td>{row.学号}</td>
                <td>{row.姓名}</td>
                <td>{row.语文}</td>
                <td>{row.数学}</td>
                <td>{row.英语}</td>
            </tr>
        """
    cont += """
        </table>
    """
    return HttpResponse("this is score" + cont)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

在这里插入图片描述

写urls

from django.contrib import admin
from django.urls import path
from antapp import views

urlpatterns = [
    path("hello/", views.hello),
    path("show_excel/",views.show_excel),
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

安装openpyxl

在这里插入图片描述

pip install openpyxl
  • 1

在这里插入图片描述

重新调试

在这里插入图片描述
以下是excel原文件

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/897249
推荐阅读
相关标签
  

闽ICP备14008679号