如何使用python将excel工作表整体导入Mysql数据库保存

2024-05-08 16:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个excel文件(.xlsx),有45列,超过1000行。我希望使用python3.5获取所有数据并将其存储在MySQL数据库中。在

我有什么完成:在

我使用openpyxl将这些值存储在python列表中,然后存储到数据库中。在

import pymysql
import openpyxl

source_excel=input('Enter the source excel file name-> ')
wb=openpyxl.load_workbook(source_excel)
sheet=wb.active

strt=int(input('Enter starting range-> '))
end=int(input('Enter ending range-> '))

name=[]

for q in range(strt,end):
    a=q,sheet.cell(row=q,column=1).value
    name.append(a)

因此,与前3行代码相同,我使用它45次,然后通过更改列号输入数据库。在

有没有办法可以将excel文件中的所有数据作为一个大容量获取并存储在Mysql中?在

另外,如果可能的话,请帮助我如何将MySQL表导出为excel文件。谢谢!在


Tags: 文件数据nameimport数据库sourceinputmysql