這篇文章將為大家詳細(xì)講解有關(guān)python3怎么實(shí)現(xiàn)mysql導(dǎo)出excel,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司是專業(yè)的任城網(wǎng)站建設(shè)公司,任城接單;提供成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行任城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!python的五大特點(diǎn)是什么python的五大特點(diǎn):1.簡(jiǎn)單易學(xué),開發(fā)程序時(shí),專注的是解決問題,而不是搞明白語言本身。2.面向?qū)ο螅c其他主要的語言如C++和Java相比, Python以一種非常強(qiáng)大又簡(jiǎn)單的方式實(shí)現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無需修改就可以在各種平臺(tái)上運(yùn)行。4.解釋性,Python語言寫的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。
Mysql中'employee'表內(nèi)容如下:
# __Desc__ = 從數(shù)據(jù)庫(kù)中導(dǎo)出數(shù)據(jù)到excel數(shù)據(jù)表中 import xlwt import pymysql class MYSQL: def __init__(self): pass def __del__(self): self._cursor.close() self._connect.close() def connectDB(self): """ 連接數(shù)據(jù)庫(kù) :return: """ try: self._connect = pymysql.Connect( host='localhost', port=3306, user='root', passwd='123456', db='test', charset='utf8' ) return 0 except: return -1 def export(self, table_name, output_path): self._cursor = self._connect.cursor() count = self._cursor.execute('select * from '+table_name) # print(self._cursor.lastrowid) print(count) # 重置游標(biāo)的位置 self._cursor.scroll(0, mode='absolute') # 搜取所有結(jié)果 results = self._cursor.fetchall() # 獲取MYSQL里面的數(shù)據(jù)字段名稱 fields = self._cursor.description workbook = xlwt.Workbook() # 注意: 在add_sheet時(shí), 置參數(shù)cell_overwrite_ok=True, 可以覆蓋原單元格中數(shù)據(jù)。 # cell_overwrite_ok默認(rèn)為False, 覆蓋的話, 會(huì)拋出異常. sheet = workbook.add_sheet('table_'+table_name, cell_overwrite_ok=True) # 寫上字段信息 for field in range(0, len(fields)): sheet.write(0, field, fields[field][0]) # 獲取并寫入數(shù)據(jù)段信息 row = 1 col = 0 for row in range(1,len(results)+1): for col in range(0, len(fields)): sheet.write(row, col, u'%s' % results[row-1][col]) workbook.save(output_path) if __name__ == '__main__': mysql = MYSQL() flag = mysql.connectDB() if flag == -1: print('數(shù)據(jù)庫(kù)連接失敗') else: print('數(shù)據(jù)庫(kù)連接成功') mysql.export('employee', 'E:/test_input.xls')
執(zhí)行結(jié)果如下:
關(guān)于“python3怎么實(shí)現(xiàn)mysql導(dǎo)出excel”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
名稱欄目:python3怎么實(shí)現(xiàn)mysql導(dǎo)出excel-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article24/cdhpje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、定制開發(fā)、ChatGPT、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容