zoukankan      html  css  js  c++  java
  • xlrd模块

    • 前端
    <div>
        <form method="post" enctype="multipart/form-data">
            <input class="form-control" type="file" name="file" value="请上传excel文件">
            <input type="submit" value="提交">
        </form>
    </div>
    
    • 后端
    import xlrd
    import pymysql
    
    
    # 建立数据库连接
    def sql_func(sql_command):
        conn = pymysql.connect(
            host = "39.107.24.78",
            port = 3306,
            user = "root",
            password = "123456",
            charset = "utf8",
            database = "flask_test"
        )
        cursor = conn.cursor(pymysql.cursors.DictCursor)    # pymysql.cursors.DictCursor 以列表套字典形式返回,默认以元组套元组返回
        sql = sql_command
        cursor.execute(sql)
        conn.commit()
        conn.close()
    
    
    def execl_input(info):
        # 找到文件
        # xls = xlrd.open_workbook("/Users/liuguixiang/Documents/excel_test.xlsx")  # 指定文件路径
        xls = xlrd.open_workbook(file_contents=info)        # 指定文件内容
    
    
        # 确定工作表
        sheet = xls.sheet_by_name("sheet1")
    
        # 遍历文件
        for i in range(2,sheet.nrows):  # 注意数据是从第几行开始的
            title = sheet.cell(i,0).value   # 取第i行,第0列,以此类推
            price = sheet.cell(i,1).value
            auther = sheet.cell(i,2).value
            sql_command = "insert into book (title,price,auther) values ('%s',%s,'%s')" %(title, price, auther)
            sql_func(sql_command)
    
    
    
  • 相关阅读:
    git 常用命令
    mac 显示隐藏文件
    android 图片缓存
    字符串与枚举相互转换
    ios 消息通知
    ios 真机调试
    ios 宏定义 系统版本 判定
    autolayout autoresizing
    c++对象创建带括号与无括号的区别
    内存对齐
  • 原文地址:https://www.cnblogs.com/os-linux/p/11892724.html
Copyright © 2011-2022 走看看