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

文章目录

安装pandas

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

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

写views

视图:

python 复制代码
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)

写urls

python 复制代码
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),
]

安装openpyxl

python 复制代码
pip install openpyxl

重新调试

以下是excel原文件

相关推荐
卡兰芙的微笑8 分钟前
get_property --Cmakelist之中
前端·数据库·编辑器
覆水难收呀10 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
猿来如此呀18 分钟前
运行npm install 时,卡在sill idealTree buildDeps没有反应
前端·npm·node.js
hw_happy23 分钟前
解决 npm ERR! node-sass 和 gyp ERR! node-gyp 报错问题
前端·npm·sass
FHKHH28 分钟前
计算机网络第二章:作业 1: Web 服务器
服务器·前端·计算机网络
视觉小鸟1 小时前
【JVM安装MinIO】
前端·jvm·chrome
二川bro1 小时前
【已解决】Uncaught RangeError: Maximum depth reached
前端
计算机学姐2 小时前
基于python+django+vue的医院预约挂号系统
开发语言·vue.js·后端·python·mysql·django·tornado
qq22951165023 小时前
python毕业设计基于django+vue医院社区医疗挂号预约综合管理系统7918h-pycharm-flask
前端·vue.js·express
八了个戒3 小时前
Koa (下一代web框架) 【Node.js进阶】
前端·node.js