python+sqlite3实现生成excel模板、解析excel文件并批量入库(最全源码)

1、生成excel模板

import pandas as pd
import sqlite3

import openpyxl

    def list(self, request, *args, **kwargs):
        data = {
            "序号":[],
            "姓名": [],
            "性别": [],
            "是否内训师": [],
            "来源": [],
            "单位名称": [],
            "部门": [],
            "职务": [],
            "手机号": [],
            "课费(元)": [],
            "身份证号": [],
            "开户行信息": [],
            "个人介绍": [],
            "备注": []
        }
        frame = pd.DataFrame(data)
        excel = frame.to_excel('example.xlsx', index=False)
        return JsonResponse(excel,safe=False)

2、解析excel文件并批量入库

 def getexcel(self, request):
        # 连接数据库
        connect = sqlite3.connect("./db.sqlite3")
        cursor = connect.cursor()
        # 读取excel文件
        workbook = openpyxl.load_workbook('./example.xlsx')
        # 获取excel文件当前表格
        active = workbook.active
        # 写sql
        insert_sql = ("insert into Teacher(name,gender,internal_trainer,source,unit_name,branch,post,"
                       "phone_num,cost,id_num,bank_info,self_intro,remark) values (?,?,?,?,?,?,?,?,?,?,?,?,?)")
        # 可以理解为裁剪表格
        for row in active.iter_rows(min_row=2,min_col=2,max_col=14, max_row=active.max_row):
            row_ = [cell.value for cell in row]
            cursor.execute(insert_sql,row_)
        connect.commit()
        connect.close()
        return JsonResponse(res201(REQUEST_PUT_SUCCESS))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tuple_Margin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值