'''
Author: liukai 2810248865@qq.com
Date: 2022-08-18 04:28:52
LastEditors: tkhywang 2810248865@qq.com
LastEditTime: 2023-08-02 18:27:08
FilePath: \PythonProject02\python 连接oracle数据库导出Excel带数据库表头.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#2022-03-31 09:52:46.000
import xlwt
import xlrd
import random
import cx_Oracle
import xlsxwriter
import chardet
sql1 = 'select T.SNO ,T.SNAME ,T.SSEX ,T.SBIRTHDAY ,T.SCLASS from student T' ###sql语句
conn = cx_Oracle.connect('C##ROOTUSER/1qaz@0987654321@IP:1521/ORCLCDB') ###连接oracle
c = conn.cursor()
filename = xlsxwriter.Workbook('student.xlsx') ###保存execel名称
x = c.execute(sql1)
title = [i[0] for i in c.description]
sheet = filename.add_worksheet()
for row in range(0, len(title)):
sheet.write(0, row, title[row])
mk = x.fetchall()
for row in range(0, len(mk)):
for col in range(0, len(mk[row])):
if type(mk[row][col]) == str:
sheet.write(row + 1, col,
mk[row][col].encode("utf-8").decode("utf-8"))
else:
sheet.write(row + 1, col, mk[row][col])
filename.close()
c.close()
conn.close()