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)
    
    
    
  • 相关阅读:
    CSS--盒子模型详解
    html元素分类
    HTML语义化(2016/3/16更新)
    如何在线预览github上的html页面?
    【鬼脸原创】谷歌扩展--知乎V2.0
    CSS选择器详解
    HTML基础知识
    python- 日志学习
    python-ddt 数据驱动测试
    python
  • 原文地址:https://www.cnblogs.com/os-linux/p/11892724.html
Copyright © 2011-2022 走看看