python操作Excel 需要导入xlrd包,可以通过pip install xlrd 一键安装。
#coding=utf-8
'''
python 将指定目录下的excel文件导入到数据库中
'''
# 引入数据库包
import pymysql
# 引入操作excel包
import xlrd
from datetime import date, datetime
# 读取excel中内容到数据库
# 从本地读取excel文件
workbook = xlrd.open_workbook('E:/upload/car.xlsx')
# 获取Excel文件的第一个模块
sheet = workbook.sheet_by_index(0)
data_list = []
row_list = []
nRows = sheet.nrows # 行数
nCols = sheet.ncols # 列数
for i in range(1, nRows):
row_list = []
for j in range(nCols):
# 获取第i行,第j列的值
data_value = sheet.cell(i, j).value
# 获取第i行,第j列的类型
# ctype : 0 empty, 1 string ,2 number, 3 date, 4 boolean 5,error
data_type = sheet.cell(i, j).ctype
if data_type == 2:
# 将字符串转为number
data_value = str(int(data_value))
if data_type == 3: