【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原文件

相关推荐
我命由我123458 小时前
CesiumJS 笔记 - 获取容器中心点、Cartesian3 clone 方法、修改 Cartesian3 对象的高度
前端·javascript·css·前端框架·html·html5·js
小白勇闯网安圈8 小时前
Django 响应对象、文件上传与类视图
数据库·django·sqlite
Hopebearer_9 小时前
页面突然只剩 DOM?一次静态资源版本错配排查
前端·部署
东风破_9 小时前
ESLint 是什么?为什么你的项目需要它?
前端·后端·代码规范
圣殿骑士-Khtangc11 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
BigTopOne11 小时前
【ijkplayer】 硬解码流程
前端
kyriewen12 小时前
DeepSeek Harness开源第一天我就上手了——和Claude Code的差距比想象中大
前端·ai编程·deepseek
捡田螺的小男孩12 小时前
什么是 Skill?手把手带你写一个简单有用的 Skill!
前端·后端·程序员
IT_陈寒12 小时前
Redis集群这个坑,差点让我通宵
前端·人工智能·后端
90后的晨仔12 小时前
uni-app 路由跳转与页面导航 —— 终极详解(对标 iOS / Android / 鸿蒙三端原生)
前端