使用xlrd读取excel

一、什么是xlrd模块?

        python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。

二、使用介绍

  1. 引入模块:import xlrd
  2. xlrd模块方法使用:如下例子“解析excel,将单元格内容按照固定格式拼装输出”,简单介绍xlrd模块使用

三、解析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+"<result column=\""+cell_value1+"\""+" property=\""+cell_value2+"\""+" />"+ "\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+"<if test=\""+cell_value2+"!=null\">and "+cell_value1+"=#{"+cell_value2+"}</if>"+ "\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+"<if test=\""+cell_value2+"!=null\">"+cell_value1+"=#{"+cell_value2+"},</if>"+ "\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-----------------------

<result column="BAE064" property="id" />
<result column="BAZ002" property="baz002" />
<result column="AAE140" property="insurance" />
<result column="AAC001" property="personId" />
<result column="AAC999" property="personNumber" />
<result column="AAC002" property="socialEnsureNumber" />
<result column="AAC003" property="name" />
<result column="AAC004" property="sex" />
<result column="AAC006" property="birthday" />
<result column="AAE035" property="transferDate" />
<result column="AAB001" property="unitNumber" />
<result column="AAC071" property="unitName" />
<result column="AAC105" property="transRegion" />
<result column="BAD699" property="payoffType" />
<result column="AAB299" property="tgtAgencyState" />
<result column="AAB301" property="srcAgencyState" />
<result column="AAB300" property="transferInAgencyName" />
<result column="AAA146" property="transferOutAgencyName" />
<result column="AAE009" property="agencyAccountName" />
<result column="AAC080" property="agencyBankNumber" />
<result column="AAC155" property="agencyBankName" />
<result column="AAC078" property="agencyAccount" />
<result column="AAE005" property="agencyPhone" />
<result column="AAE006" property="agencyAddress" />
<result column="AAE007" property="agencyZip" />
<result column="AAE072" property="billCode" />
<result column="BAD312" property="intoAccountStatus" />
<result column="BZZ100" property="backStatus" />
<result column="AIC083" property="personActFundTransferSum" />
<result column="AIC084" property="regionFundTransferSum" />
<result column="AIC102" property="transferFundSum" />
<result column="BAE062" property="transEventId" />
<result column="AAE013" property="remark" />
<result column="BAE969" property="payType" />
<result column="BAD435" property="failReason" />
<result column="AAE011" property="aae011" />
<result column="AAE036" property="aae036" />
<result column="AAB034" property="aab034" />
<result column="AAA027" property="aaa027" />
<result column="BZE011" property="bze011" />
<result column="BZE036" property="bze036" />
<result column="BZE034" property="bze034" />

--------------------part2-----------------------

<if test="id!=null">and BAE064=#{id}</if>
<if test="baz002!=null">and BAZ002=#{baz002}</if>
<if test="insurance!=null">and AAE140=#{insurance}</if>
<if test="personId!=null">and AAC001=#{personId}</if>
<if test="personNumber!=null">and AAC999=#{personNumber}</if>
<if test="socialEnsureNumber!=null">and AAC002=#{socialEnsureNumber}</if>
<if test="name!=null">and AAC003=#{name}</if>
<if test="sex!=null">and AAC004=#{sex}</if>
<if test="birthday!=null">and AAC006=#{birthday}</if>
<if test="transferDate!=null">and AAE035=#{transferDate}</if>
<if test="unitNumber!=null">and AAB001=#{unitNumber}</if>
<if test="unitName!=null">and AAC071=#{unitName}</if>
<if test="transRegion!=null">and AAC105=#{transRegion}</if>
<if test="payoffType!=null">and BAD699=#{payoffType}</if>
<if test="tgtAgencyState!=null">and AAB299=#{tgtAgencyState}</if>
<if test="srcAgencyState!=null">and AAB301=#{srcAgencyState}</if>
<if test="transferInAgencyName!=null">and AAB300=#{transferInAgencyName}</if>
<if test="transferOutAgencyName!=null">and AAA146=#{transferOutAgencyName}</if>
<if test="agencyAccountName!=null">and AAE009=#{agencyAccountName}</if>
<if test="agencyBankNumber!=null">and AAC080=#{agencyBankNumber}</if>
<if test="agencyBankName!=null">and AAC155=#{agencyBankName}</if>
<if test="agencyAccount!=null">and AAC078=#{agencyAccount}</if>
<if test="agencyPhone!=null">and AAE005=#{agencyPhone}</if>
<if test="agencyAddress!=null">and AAE006=#{agencyAddress}</if>
<if test="agencyZip!=null">and AAE007=#{agencyZip}</if>
<if test="billCode!=null">and AAE072=#{billCode}</if>
<if test="intoAccountStatus!=null">and BAD312=#{intoAccountStatus}</if>
<if test="backStatus!=null">and BZZ100=#{backStatus}</if>
<if test="personActFundTransferSum!=null">and AIC083=#{personActFundTransferSum}</if>
<if test="regionFundTransferSum!=null">and AIC084=#{regionFundTransferSum}</if>
<if test="transferFundSum!=null">and AIC102=#{transferFundSum}</if>
<if test="transEventId!=null">and BAE062=#{transEventId}</if>
<if test="remark!=null">and AAE013=#{remark}</if>
<if test="payType!=null">and BAE969=#{payType}</if>
<if test="failReason!=null">and BAD435=#{failReason}</if>
<if test="aae011!=null">and AAE011=#{aae011}</if>
<if test="aae036!=null">and AAE036=#{aae036}</if>
<if test="aab034!=null">and AAB034=#{aab034}</if>
<if test="aaa027!=null">and AAA027=#{aaa027}</if>
<if test="bze011!=null">and BZE011=#{bze011}</if>
<if test="bze036!=null">and BZE036=#{bze036}</if>
<if test="bze034!=null">and BZE034=#{bze034}</if>

--------------------part3-----------------------

<if test="id!=null">BAE064=#{id},</if>
<if test="baz002!=null">BAZ002=#{baz002},</if>
<if test="insurance!=null">AAE140=#{insurance},</if>
<if test="personId!=null">AAC001=#{personId},</if>
<if test="personNumber!=null">AAC999=#{personNumber},</if>
<if test="socialEnsureNumber!=null">AAC002=#{socialEnsureNumber},</if>
<if test="name!=null">AAC003=#{name},</if>
<if test="sex!=null">AAC004=#{sex},</if>
<if test="birthday!=null">AAC006=#{birthday},</if>
<if test="transferDate!=null">AAE035=#{transferDate},</if>
<if test="unitNumber!=null">AAB001=#{unitNumber},</if>
<if test="unitName!=null">AAC071=#{unitName},</if>
<if test="transRegion!=null">AAC105=#{transRegion},</if>
<if test="payoffType!=null">BAD699=#{payoffType},</if>
<if test="tgtAgencyState!=null">AAB299=#{tgtAgencyState},</if>
<if test="srcAgencyState!=null">AAB301=#{srcAgencyState},</if>
<if test="transferInAgencyName!=null">AAB300=#{transferInAgencyName},</if>
<if test="transferOutAgencyName!=null">AAA146=#{transferOutAgencyName},</if>
<if test="agencyAccountName!=null">AAE009=#{agencyAccountName},</if>
<if test="agencyBankNumber!=null">AAC080=#{agencyBankNumber},</if>
<if test="agencyBankName!=null">AAC155=#{agencyBankName},</if>
<if test="agencyAccount!=null">AAC078=#{agencyAccount},</if>
<if test="agencyPhone!=null">AAE005=#{agencyPhone},</if>
<if test="agencyAddress!=null">AAE006=#{agencyAddress},</if>
<if test="agencyZip!=null">AAE007=#{agencyZip},</if>
<if test="billCode!=null">AAE072=#{billCode},</if>
<if test="intoAccountStatus!=null">BAD312=#{intoAccountStatus},</if>
<if test="backStatus!=null">BZZ100=#{backStatus},</if>
<if test="personActFundTransferSum!=null">AIC083=#{personActFundTransferSum},</if>
<if test="regionFundTransferSum!=null">AIC084=#{regionFundTransferSum},</if>
<if test="transferFundSum!=null">AIC102=#{transferFundSum},</if>
<if test="transEventId!=null">BAE062=#{transEventId},</if>
<if test="remark!=null">AAE013=#{remark},</if>
<if test="payType!=null">BAE969=#{payType},</if>
<if test="failReason!=null">BAD435=#{failReason},</if>
<if test="aae011!=null">AAE011=#{aae011},</if>
<if test="aae036!=null">AAE036=#{aae036},</if>
<if test="aab034!=null">AAB034=#{aab034},</if>
<if test="aaa027!=null">AAA027=#{aaa027},</if>
<if test="bze011!=null">BZE011=#{bze011},</if>
<if test="bze036!=null">BZE036=#{bze036},</if>
<if test="bze034!=null">BZE034=#{bze034},</if>

--------------------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},

喜欢我的文章希望和我一起成长的宝宝们,可以搜索并添加公众号TryTestwonderful ,或者扫描下方二维码添加公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半夏映浮光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值