当前位置:   article > 正文

uniapp+flask+pandas批量上传处理excel数据_flask 上传excel

flask 上传excel

uniapp里面,我们可以先写一个按钮,绑定上传事件

<button @click="test">上传Excel,批量上传</button>
  • 1

在methods里面,使用uni自带的函数,uni.chooseFile和uni.uploadFile

uni.chooseFile用来选择文件,uni.uploadFile用来上传文件

uni.chooseFile({
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
					uni.uploadFile({
						url: 'url', //仅为示例,非真实的接口地址 
						filePath: tempFilePaths[0],
						name: 'file',
						formData:{list_id:this.listid},  //携带的参数
						success: (uploadFileRes) => {
						console.log(uploadFileRes.data);
				}
							      });
						
				});
					
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

此时向服务器发送的是,带有二进制的文件。

在这里插入图片描述

python先用pip安装好 flask、pandas、openpyxl

from flask import Flask,render_template,request,g,session,url_for,send_file
import MySQLdb
import pandas as pd

#MySQL配置
host = 'localhost'
user = 'root'
password = '123'
database = 'pro'

app = Flask(__name__) 



@app.route('/addexcel', methods=['GET', 'POST'])   
def excel():
    db = MySQLdb.connect(host=host, user=user, passwd=password, db=database)
    cursor = db.cursor()
    
    #获取excel文件
    file = request.files['file']
    
    #获取参数,可选
    list_id = request.values.get("list_id")
    

    #pd读取excel
    df = pd.read_excel(file)
    
   
    
    
     
    #df.index.values,返回的是每行index的数组
    for i in df.index.values:
       #df.iloc可以根据下标和列名取元素,df[下标][列名]
        que = str(df.iloc[i]["que"])
        a = str(df.iloc[i]["a"])
        b = str(df.iloc[i]["b"])
        c = str(df.iloc[i]["c"])
        d = str(df.iloc[i]["d"])
        ans = str(df.iloc[i]["ans"])
        
        
		#上传到数据库即可
        cursor.execute("REPLACE into que(que,list_id,a,b,c,d,ans) values(%s,%s,%s,%s,%s,%s,%s)",(que,list_id,a,b,c,d,ans,))
        
        db.commit()
        
    
        

  
    return "200"
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/613866
推荐阅读
相关标签
  

闽ICP备14008679号