一、报错:
File "G:/station/6系列装置信息明细.py", line 12, in <module>workbook.save(r'.\6系列装置明细表.xls')File "G:\python\lib\site-packages\xlwt\Workbook.py", line 710, in savedoc.save(filename_or_stream, self.get_biff_data())File "G:\python\lib\site-packages\xlwt\Workbook.py", line 680, in get_biff_dataself.worksheets[self.active_sheet].selected = TrueIndexError: list index out of range此问题是由于未能正确使用价表工具导致;二、数据插入excelimport xlwt workbook = xlwt.Workbook('6系列装置明细.xls') #给表名命sheet1 = workbook.add_sheet('6系列') #给表的sheet1名命(如果只有一页内容应该用不上这个)k = 0for i in A:device = i[0]print(device)SIM = i[1]sheet1.write(k,0,device) #k为行 ,0为列 ,后面是要插入的内容sheet1.write(k,1,SIM)k = k + 1workbook.save('6系列装置明细.xls') 用此代码确实可以实现目的,但是查看数据量只有98行,经过查看,是由于for循环在上一个for循环之外,我用的是连接多个系统,导致没有将所有的数据导入表格。三、报错Exception: Attempt to overwrite cell: sheetname='6系列' rowx=0 colx=0这个问题是由于循环中没有处理号行号,导致每一次换系统拿出来的数据都以第一行开始写入;四、代码:-- coding:UTF-8 --import osimport pymssqlimport xlwtworkbook = xlwt.Workbook('已更换6系列装置明细')sheet1 = workbook.add_sheet('6系列')sql_name = [['*','sa','*','TAS3.2_DB_JLNA'],['','sa','','TAS3.2_DB_NMBY'],['','sa','','TAS3.2_DB_NMWHLWS'],['','sa','','TAS3.2_DB_NX'],]print('**程序正在启动****')os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'k = 0for SJK in sql_name: IP_DB = SJK[0] # 获取数据库IP us_DB = SJK[1] PW_DB= SJK[2] DB = SJK[3] DB_name = DB[10:] print(DB)conn = pymssql.connect(host=IP_DB, user=us_DB, password=PW_DB, database=DB)cur = conn.cursor()check_sql = "SELECT sFactoryNum,sGSMcardNum FROM XHQD620_Device WHERE lDevStatus = 8" #DB库中拿装置编号与SIM号cur.execute(check_sql)A = cur.fetchall()print(A)for i in A: device = i[0] print(device) SIM = i[1] sheet1.write(k,0,device) sheet1.write(k,1,SIM) k = k + 1workbook.save('已更换6系列装置明细.xls')