一、什么是xlrd模块?
python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。
二、使用介绍
三、解析excel,将单元格内容按照固定格式拼装输出
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 9 20:42:47 2019
@author: hx
解析excel,将单元格内容按照固定格式拼装输出
"""
import xlrd
def resolveExcel():
# 获取excel文件
data = xlrd.open_workbook("C:/Users/Administrator/Desktop/test.xls",encoding_override='utf-8')
#获取一个excel有多少个sheet
sheetNames = list(data.sheet_names())
print(sheetNames)
#遍历sheet
for name in sheetNames:
# 获取sheet
sheet = data.sheet_by_name(name)
#打印拼装的resultMap
packing_part1_resultMap(sheet)
#打印拼装的where条件
packing_part2_where_criteria(sheet)
#打印拼装的update条件
packing_part3_update_criteria(sheet)
#打印拼装的insert条件
packing_part4_insert_criteria(sheet)
# 获取一行的数值
#table.row_values(i)
# 获取一列的数值
#key = sheet.col_values(0)
#chinese = sheet.col_values(1)
#获取具体单元格的值
# cell_value = table.cell(0,1).value
# print(cell_value)
#chinestfile = open('/aim/file/location/?.txt', 'a+', encoding='utf-8')
#chinestfile.write(chineseStr)
def packing_part1_resultMap(sheet):
# 获取总行数
nrows = sheet.nrows
chineseStr = ""
#循环行数
for row in range(nrows):
#第一行不计入
if row !=0:
#取第一列值
cell_value1=sheet.cell(row,0).value
#取第二列值
cell_value2=sheet.cell(row,1).value
chineseStr=chineseStr+" "+ "\n"
print(chineseStr)
def packing_part2_where_criteria(sheet):
# 获取总行数
nrows = sheet.nrows
chineseStr = ""
#循环行数
for row in range(nrows):
#第一行不计入
if row !=0:
#取第一列值
cell_value1=sheet.cell(row,0).value
#取第二列值
cell_value2=sheet.cell(row,1).value
chineseStr=chineseStr+"and "+cell_value1+"=#{"+cell_value2+"} "+ "\n"
print(chineseStr)
def packing_part3_update_criteria(sheet):
# 获取总行数
nrows = sheet.nrows
chineseStr = ""
#循环行数
for row in range(nrows):
#第一行不计入
if row !=0:
#取第一列值
cell_value1=sheet.cell(row,0).value
#取第二列值
cell_value2=sheet.cell(row,1).value
chineseStr=chineseStr+""+cell_value1+"=#{"+cell_value2+"}, "+ "\n"
print(chineseStr)
def packing_part4_insert_criteria(sheet):
# 获取总行数
nrows = sheet.nrows
chineseStr = ""
#循环行数
for row in range(nrows):
#第一行不计入
if row !=0:
#取第一列值
#cell_value1=sheet.cell(row,0).value
#取第二列值
cell_value2=sheet.cell(row,1).value
chineseStr=chineseStr+"#{"+cell_value2+",jdbcType=VARCHAR},"+ "\n"
print(chineseStr)
if __name__ == '__main__':
resolveExcel()
四、输出样例
--------------------part1-----------------------
--------------------part2-----------------------
--------------------part3-----------------------
--------------------part4-----------------------
#{id,jdbcType=VARCHAR},
#{baz002,jdbcType=VARCHAR},
#{insurance,jdbcType=VARCHAR},
#{personId,jdbcType=VARCHAR},
#{personNumber,jdbcType=VARCHAR},
#{socialEnsureNumber,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{birthday,jdbcType=VARCHAR},
#{transferDate,jdbcType=VARCHAR},
#{unitNumber,jdbcType=VARCHAR},
#{unitName,jdbcType=VARCHAR},
#{transRegion,jdbcType=VARCHAR},
#{payoffType,jdbcType=VARCHAR},
#{tgtAgencyState,jdbcType=VARCHAR},
#{srcAgencyState,jdbcType=VARCHAR},
#{transferInAgencyName,jdbcType=VARCHAR},
#{transferOutAgencyName,jdbcType=VARCHAR},
#{agencyAccountName,jdbcType=VARCHAR},
#{agencyBankNumber,jdbcType=VARCHAR},
#{agencyBankName,jdbcType=VARCHAR},
#{agencyAccount,jdbcType=VARCHAR},
#{agencyPhone,jdbcType=VARCHAR},
#{agencyAddress,jdbcType=VARCHAR},
#{agencyZip,jdbcType=VARCHAR},
#{billCode,jdbcType=VARCHAR},
#{intoAccountStatus,jdbcType=VARCHAR},
#{backStatus,jdbcType=VARCHAR},
#{personActFundTransferSum,jdbcType=VARCHAR},
#{regionFundTransferSum,jdbcType=VARCHAR},
#{transferFundSum,jdbcType=VARCHAR},
#{transEventId,jdbcType=VARCHAR},
#{remark,jdbcType=VARCHAR},
#{payType,jdbcType=VARCHAR},
#{failReason,jdbcType=VARCHAR},
#{aae011,jdbcType=VARCHAR},
#{aae036,jdbcType=VARCHAR},
#{aab034,jdbcType=VARCHAR},
#{aaa027,jdbcType=VARCHAR},
#{bze011,jdbcType=VARCHAR},
#{bze036,jdbcType=VARCHAR},
#{bze034,jdbcType=VARCHAR},