修改了excel解析的view、litem_list
This commit is contained in:
parent
365ad8d2b6
commit
faa05ab94d
|
@ -4,7 +4,6 @@
|
|||
<html lang="en">
|
||||
|
||||
<head>
|
||||
|
||||
<title>Excel解析工具</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
|
||||
|
@ -21,64 +20,53 @@
|
|||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="page-wrapper">
|
||||
<form id="preview-form" method="post">
|
||||
<input type="hidden" name="csrfmiddlewaretoken" value="sNsa35OBeT7kQBrftJ8RaPHpICVfRsUgxshXt37Zc18ERTa4Ku4LQR3VJNTFGiSu">
|
||||
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="excelPreviewModalTitle">上传文件预览</h5>
|
||||
<h5 class="modal-title" id="excelPreviewModalTitle">上传文件预览</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div style="overflow-x: auto;">
|
||||
<table id="form-input-table" class="table table-striped table-bordered nowrap">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>序号</th>
|
||||
<th>车牌</th>
|
||||
<th>借出时间</th>
|
||||
<th>借用人</th>
|
||||
<th>同行人员</th>
|
||||
<th>事由</th>
|
||||
<th>目的地</th>
|
||||
<th>用车天数</th>
|
||||
<th>还车时间</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="license_plate" name="license_plate" value="川A77889"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="checkout_time" name="checkout_time" value="2024-01-31"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="borrower" name="borrower" value="张三"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="accompanying_personnel" name="accompanying_personnel" value="李四"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="reason" name="reason" value="外出见客户"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="destination" name="destination" value="金融麦田"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="number" class="form-control" id="days_of_use" name="days_of_use" value="1"></td>
|
||||
<td>
|
||||
<input style="width: 180px" type="text" class="form-control" id="return_time" name="return_time" value="2024-02-01"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div style="overflow-x: auto;">
|
||||
<table id="form-input-table" class="table table-striped table-bordered nowrap">
|
||||
<thead>
|
||||
<tr>
|
||||
{% for column in columns %}
|
||||
<th>{{ column }}</th>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in table_data %}
|
||||
<tr>
|
||||
{% for cell in row %}
|
||||
<td>
|
||||
<input type="text" class="form-control" style="width: 180px;" name="cell_{{ forloop.parentloop.counter0 }}_{{ forloop.counter0 }}" value="{{ cell }}">
|
||||
</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<div id="pagination">
|
||||
<span id="totalData" class="mx-2">数据总数: 1</span>
|
||||
<span id="pageInfo" class="mx-2">第 1 页, 共 1 页</span>
|
||||
<button id="prevPage" class="btn btn-secondary" type="button">上一页</button>
|
||||
<button id="nextPage" class="btn btn-secondary" type="button">下一页</button></div>
|
||||
<div id="actions">
|
||||
<button id="saveButton" type="button" class="btn btn-primary">保存</button></div>
|
||||
<div id="pagination">
|
||||
<span id="totalData" class="mx-2">数据总数: {{ table_data|length }}</span>
|
||||
<span id="pageInfo" class="mx-2">第 1 页, 共 1 页</span>
|
||||
<button id="prevPage" class="btn btn-secondary" type="button">上一页</button>
|
||||
<button id="nextPage" class="btn btn-secondary" type="button">下一页</button>
|
||||
</div>
|
||||
<div id="actions">
|
||||
<button id="saveButton" type="button" class="btn btn-primary">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script src="{% static 'js/vendor-all.min.js' %}"></script>
|
||||
<script src="{% static 'plugins/bootstrap/js/bootstrap.min.js' %}"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -3,7 +3,7 @@ from datetime import datetime
|
|||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.staticfiles import finders
|
||||
from django.http import HttpResponseBadRequest, JsonResponse, FileResponse, HttpResponseNotFound
|
||||
from django.http import JsonResponse, FileResponse
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
from openpyxl.reader.excel import load_workbook
|
||||
|
@ -40,10 +40,30 @@ def dl_excel_tpl(request, template_name):
|
|||
|
||||
@login_required
|
||||
def common_parse(request):
|
||||
"""
|
||||
处理上传的 Excel 文件,与模板对比表头并解析数据。
|
||||
|
||||
如果请求方法为 POST,会执行以下步骤:
|
||||
1. 获取上传的 Excel 文件和模板名称。
|
||||
2. 加载上传的 Excel 文件并获取其表头。
|
||||
3. 找到并加载对应的模板文件,获取模板表头。
|
||||
4. 对比上传文件和模板的表头,若不一致则返回错误信息。
|
||||
5. 解析上传文件中的数据,处理 datetime 对象并过滤空行。
|
||||
6. 将解析后的数据存储在会话中。
|
||||
7. 返回预览页面的 URL。
|
||||
|
||||
参数:
|
||||
request (HttpRequest): Django 的 HttpRequest 对象,包含请求的详细信息。
|
||||
|
||||
返回:
|
||||
JsonResponse: JSON 响应,包含预览页面的重定向 URL 或错误信息。
|
||||
"""
|
||||
if request.method == 'POST':
|
||||
# 获取上传的 Excel 文件和模板名称
|
||||
excel_file = request.FILES['file']
|
||||
template_name = request.POST.get('template_name')
|
||||
|
||||
# 加载上传的 Excel 文件
|
||||
wb = load_workbook(excel_file)
|
||||
sheet = wb.active
|
||||
|
||||
|
@ -89,7 +109,23 @@ def common_parse(request):
|
|||
|
||||
@login_required
|
||||
def excel_preview(request):
|
||||
"""
|
||||
预览 Excel 文件的视图函数。该函数从会话中获取列名和表格数据,并将其渲染到 HTML 模板中。
|
||||
|
||||
预期的会话数据:
|
||||
- 'columns': 包含列名的列表。
|
||||
- 'table_data': 包含表格数据的列表。
|
||||
|
||||
参数:
|
||||
request (HttpRequest): Django 的 HttpRequest 对象,包含请求的详细信息。
|
||||
|
||||
返回:
|
||||
HttpResponse: 渲染 'excel_preview_table.html' 模板,并包含会话中的列名和表格数据。
|
||||
"""
|
||||
# 从会话中获取列名列表
|
||||
columns = request.session.get('columns', [])
|
||||
# 从会话中获取表格数据列表
|
||||
table_data = request.session.get('table_data', [])
|
||||
|
||||
# 渲染 HTML 模板 'excel_preview_table.html',并传递列名和表格数据
|
||||
return render(request, 'excel_preview_table.html', {'columns': columns, 'table_data': table_data})
|
||||
|
|
|
@ -138,7 +138,6 @@
|
|||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
<div id="addEditModal" class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog"
|
||||
aria-labelledby="formModalCenterTitle" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered modal-xl" role="document">
|
||||
|
|
Loading…
Reference in New Issue