1. 解决思路:
1) 使用python读取excel表格中数据
2)根据数据生成SQL语句
3)批量运行SQL语句
2. Python代码:
import pandas as pd
def readExcel(path, excel_file):
return pd.read_excel(path + excel_file)
if __name__ == '__main__':
file_path = '修改为自己的文件路径'
# 文件名
file_name = 'user.xlsx'
sheet = readExcel(file_path, file_name)
print(sheet)
print(sheet.shape)
# 获取excel表格中的行和列数
print(sheet.shape[0], sheet.shape[1])
user = []
for i in range(0, sheet.shape[0]):
for j in range(0, sheet.shape[1]):
user.append(str(sheet.iloc[i, j]))
# 生成sql语句
print("insert into user(id, name, age, email) "
"values(" + str(user[0]) + ", " + str(user[1]) + ", " + str(user[2]) + ", " + str(user[3]) + ")")
user.clear()
3. 运行结果: