读取excel数据并存入数据库

读取excel数据并存入数据库

全部字段插入数据

import openpyxl
from openpyxl import Workbook
import pymysql


class Test:
    # 新建一个excel文件并写入内容,用的是openpyxl的方法
    def write_file(self):
        # 创建一个工作簿
        book = Workbook()
        # 激活一个工作表
        sheet = book.active
        # 设置工作表名称
        sheet.title = "测试文档"
        # 往文档里面写入数据
        # 指定单元格写入数据
        # sheet['A1']="name"
        # sheet.cell(row=1,column=2).value="age"
        # 多条数据插入:append
        rows = (
            ("id", "name", "age"),
            (1, "往", 23),
            (2, "石", 33)
        )
        for row in rows:
            sheet.append(row)
        book.save('测试.xlsx')

    # 读取excel文件内容
    # 读取excel文件内容,连接数据库,并将数据写入数据库
    def read_file(self):
        # 利用load_workbook()方法打开文件
        workbook = openpyxl.load_workbook('测试.xlsx')
        # 获取第一个sheet
        sheet = workbook.active
        # 按行的形式读取数据信息
        for row in sheet.iter_rows(min_row=2, values_only=True):
            # print(row)
            yield row

    # 连接数据库
    def write_sql(self):
        # 连接数据库
        db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', database='wgz')
        # 开启游标功能,创建游标对象,创建cursor()游标对象
        cuisor = db.cursor()
        # 创建一个新增的sql
        insert_sql = 'insert into name values(%s,%s,%s)'
        # 获取excel的表格内容
        result = self.read_file()
        # 遍历excel内容
        for date in result:
            cuisor.execute(insert_sql,date)
        # 数据提交
        db.commit()
        # 查询添加的数据信息
        select_sql = 'select * from name'
        cuisor.execute(select_sql)
        data = cuisor.fetchall()
        for i in data:
            print("查询的数据为:",i)


if __name__ == '__main__':
    Test().write_sql()

指定字段插入数据

import openpyxl
from openpyxl import Workbook
import pymysql


class Test:
    # 新建一个excel文件并写入内容,用的是openpyxl的方法
    def write_file(self):
        # 创建一个工作簿
        book = Workbook()
        # 激活一个工作表
        sheet = book.active
        # 设置工作表名称
        sheet.title = "测试文档"
        # 往文档里面写入数据
        # 指定单元格写入数据
        # sheet['A1']="name"
        # sheet.cell(row=1,column=2).value="age"
        # 多条数据插入:append
        rows = (
            ("id", "name", "age"),
            (3, "往", 23),
            (4, "石", 33)
        )
        for row in rows:
            sheet.append(row)
        book.save('测试1.xlsx')

    # 读取excel文件内容
    # 读取excel文件内容,连接数据库,并将数据写入数据库
    def read_file(self):
        self.write_file()
        # 利用load_workbook()方法打开文件
        workbook = openpyxl.load_workbook('测试1.xlsx')
        # 获取第一个sheet
        sheet = workbook.active
        # 按行的形式读取数据信息
        for row in sheet.iter_rows(min_row=2):
            # 读取指定的列数
            a = row[0].value
            b = row[1].value
            # 读取的内容转化为元组
            tup = (a, b)
            # 将元组进行迭代返回
            yield tup

    # 连接数据库
    def write_sql(self):
        # 连接数据库
        db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', database='wgz')
        # 开启游标功能,创建游标对象,创建cursor()游标对象
        cuisor = db.cursor()
        # 创建一个新增的sql
        insert_sql = 'insert into name(id,name) values(%s,%s)'
        # 获取excel的表格内容
        result = self.read_file()
        # 遍历excel内容
        for date in result:
            cuisor.execute(insert_sql, date)
        # 数据提交
        db.commit()
        # 查询添加的数据信息
        select_sql = 'select * from name'
        cuisor.execute(select_sql)
        data = cuisor.fetchall()
        for i in data:
            print("查询的数据为:", i)


if __name__ == '__main__':
    Test().write_sql()

posted @ 2023-11-27 09:30  没事别找我  阅读(50)  评论(0编辑  收藏  举报