請你幫我檢from flask import Flask, render_template, request, redirect import openpyxl app = Flask(__name__) # 首页,录入数据页面 @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': # 从表单中获取数据 confirm_date = request.form['confirm_date'] shift = request.form['shift'] machine_model = request.form['machine_model'] issue = request.form['issue'] issue_qty = request.form['issue_qty'] note = request.form['note'] # 打开Excel文件并写入数据 wb = openpyxl.load_workbook('D:/data.xlsx') ws = wb.active row_count = ws.max_row ws.cell(row=row_count+1, column=1, value=confirm_date) ws.cell(row=row_count+1, column=2, value=shift) ws.cell(row=row_count+1, column=3, value=machine_model) ws.cell(row=row_count+1, column=4, value=issue) ws.cell(row=row_count+1, column=5, value=int(issue_qty)) ws.cell(row=row_count+1, column=6, value=note) wb.save('D:/data.xlsx') return redirect('/') else: return render_template('index.html') # 查询记录页面 @app.route('/search_record', methods=['GET', 'POST']) def search_record(): if request.method == 'POST': # 从表单中获取查询条件 start_date = request.form['start_date'] end_date = request.form['end_date'] machine_model = request.form['machine_model'] # 打开Excel文件并查询数据 wb = openpyxl.load_workbook('D:/data.xlsx') ws = wb.active data = [] for row in ws.iter_rows(min_row=2): if start_date <= str(row[0].value) <= end_date: if row[2].value == machine_model or machine_model == '全部': data.append([row[0].value, row[1].value, row[2].value, row[3].value, row[4].value, row[5].value]) return render_template('search_record.html', data=data) else: return render_template('search_record.html') if __name__ == '__main__': app.run(debug=True,host="0.0.0.0")
时间: 2024-03-19 07:45:02 浏览: 147
这段代码看起来没有语法错误,但需要确保您已经正确安装了 Flask 和 openpyxl 库。此外,需要确保您的 Excel 文件 'D:/data.xlsx' 存在并且可写。在使用 openpyxl 时,建议您使用上下文管理器来打开并保存文件,这可以确保在使用文件后正确关闭文件。下面是更新后的代码:
```
from flask import Flask, render_template, request, redirect
import openpyxl
app = Flask(__name__)
# 首页,录入数据页面
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# 从表单中获取数据
confirm_date = request.form['confirm_date']
shift = request.form['shift']
machine_model = request.form['machine_model']
issue = request.form['issue']
issue_qty = request.form['issue_qty']
note = request.form['note']
# 打开Excel文件并写入数据
with openpyxl.load_workbook('D:/data.xlsx') as wb:
ws = wb.active
row_count = ws.max_row
ws.cell(row=row_count+1, column=1, value=confirm_date)
ws.cell(row=row_count+1, column=2, value=shift)
ws.cell(row=row_count+1, column=3, value=machine_model)
ws.cell(row=row_count+1, column=4, value=issue)
ws.cell(row=row_count+1, column=5, value=int(issue_qty))
ws.cell(row=row_count+1, column=6, value=note)
wb.save('D:/data.xlsx')
return redirect('/')
else:
return render_template('index.html')
# 查询记录页面
@app.route('/search_record', methods=['GET', 'POST'])
def search_record():
if request.method == 'POST':
# 从表单中获取查询条件
start_date = request.form['start_date']
end_date = request.form['end_date']
machine_model = request.form['machine_model']
# 打开Excel文件并查询数据
with openpyxl.load_workbook('D:/data.xlsx') as wb:
ws = wb.active
data = []
for row in ws.iter_rows(min_row=2):
if start_date <= str(row[0].value) <= end_date:
if row[2].value == machine_model or machine_model == '全部':
data.append([row[0].value, row[1].value, row[2].value, row[3].value, row[4].value, row[5].value])
return render_template('search_record.html', data=data)
else:
return render_template('search_record.html')
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0")
```
注意:在生产环境中,不建议使用 Flask 的调试模式。如果您要在生产环境中运行应用程序,请将 `debug=True` 更改为 `debug=False`。
阅读全文