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

相关推荐
JavaGuide7 小时前
DeepSeek Harness 官方桌面端终于有了!
前端·后端
wshzd7 小时前
LLM之Agent(104)|DeepSeek-Harness(十三)ReactLoopAgent 总览:kick → turn → step
开发语言·前端·javascript
茉莉玫瑰花茶7 小时前
OpenGL [ 基础概念 ]
java·前端·数据库
随性而行3607 小时前
企业微信二次开发如何接入大模型工具?API接口实现智能任务调用的技术思路
java·前端·人工智能·python·微信·机器人·企业微信
IMPYLH7 小时前
HTML 的 <td> 元素
前端·html
gnip7 小时前
Flutter GetX 三件套开发规范(Skill)
前端·flutter
张小姐的猫7 小时前
【AI大模型接入SDK】 —— 前端页面 & 项目总结与拓展
前端·数据结构·数据库·c++·人工智能·chatgpt
web打印社区8 小时前
远程打印:WebSocket 与 HTTP 轮询怎么选
前端·vue.js·websocket·网络协议·http·electron·pdf
吴声子夜歌8 小时前
HTML——庞杂的表单控件元素(一)
前端·html
我的愿望是成为富婆8 小时前
HR数字化校招技术栈拆解:SQL、Excel、Power BI和AI工具在2026届JD中的真实权重
人工智能·sql·excel