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

相关推荐
zengyuhan50310 小时前
Windows BLE 开发指南(Rust windows-rs)
前端·rust
醉方休10 小时前
Webpack loader 的执行机制
前端·webpack·rust
前端老宋Running10 小时前
一次从“卡顿地狱”到“丝般顺滑”的 React 搜索优化实战
前端·react.js·掘金日报
隔壁的大叔10 小时前
如何自己构建一个Markdown增量渲染器
前端·javascript
用户44455436542610 小时前
Android的自定义View
前端
WILLF10 小时前
HTML iframe 标签
前端·javascript
程序设计实验室10 小时前
性能提升4倍!使用Granian作为Django项目的ASGI服务器
django·djangostarter
枫,为落叶11 小时前
Axios使用教程(一)
前端
小章鱼学前端11 小时前
2025 年最新 Fabric.js 实战:一个完整可上线的图片选区标注组件(含全部源码).
前端·vue.js
ohyeah11 小时前
JavaScript 词法作用域、作用域链与闭包:从代码看机制
前端·javascript