使用python读取excel文件并打印到控制台上
导包
import openpyxl
找excel文件
workbook = openpyxl.load_workbook('苏州.xlsx')
sheet = workbook.active
读取excel文件的列名
columns = []
for col in range(2, sheet.max_column + 1):
cell = sheet.cell(1, col)
columns.append(cell.value)
全部代码
import openpyxl
workbook = openpyxl.load_workbook('苏州.xlsx')
sheet = workbook.active
columns = []
for col in range(2, sheet.max_column + 1):
cell = sheet.cell(1, col)
columns.append(cell.value)
for row in range(2, sheet.max_row + 1):
print(" INSERT INTO 0823_02 (", end='')
for i in range(len(columns)):
if i < len(columns) - 1:
print(columns[i] + ", ", end='')
else:
print(columns[i]+") VALUES (", end='')
for col in range(2, sheet.max_column + 1):
cell = sheet.cell(row, col)
print("'"+str(cell.value)+"'", end='')
if col < sheet.max_column:
print(", ", end='')
print(");")