本文示例功能点
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import os import ibm_db from openpyxl import Workbook
db_url = "DATABASE=iscfdb;SCHEMA=IOP;HOSTNAME=199.188.166.110;PORT=60006;PROTOCOL=TCPIP;UID=excel;PWD=excel;"
select_sql = """select * from IOP.TSF_INSTANCE """
file_name = "TSF_INSTANCE.xlsx"
if os.path.exists(file_name): os.remove(file_name)
wb = Workbook()
ws = wb.create_sheet('IOP.TSF_INSTANCE', 0)
try: conn = ibm_db.connect(db_url, "", "") ibm_db.autocommit(conn, ibm_db.SQL_AUTOCOMMIT_OFF) stmt = ibm_db.exec_immediate(conn, select_sql) res = ibm_db.fetch_tuple(stmt) num = 0 while (res): num += 1 print("第" + str(num) + "行写入成功") ws.append(res) res = ibm_db.fetch_tuple(stmt) print("文件写入完成,开始保存……") wb.save(file_name) wb.close() ibm_db.commit(conn) except Exception as e: print(e) ibm_db.rollback(conn) finally: ibm_db.close(conn)
if not os.path.exists(file_name): print("生成Excel失败") else: print("生成Excel成功")
|
蚂蚁🐜再小也是肉🥩!