将数据库数据导出为excel表格是一个常见且实用的功能,特别是对于不熟悉数据库操作语句的用户而言。下面我们来看看实现效果。
数据源

导出结果

立即学习“Python免费学习笔记(深入)”;
依赖
由于使用Python实现,所以需要Python环境的支持。
我的Python环境是2.7.11。虽然你可能使用的是3.5版本,但实现思路是一致的。
xlwt
<pre class="brush:php;toolbar:false;">pip install xlwt
MySQLdb
<pre class="brush:php;toolbar:false;">pip install MySQLdb
如果上述方式不成功,可以到sourceforge官网下载Windows上的msi版本或使用源码自行编译。
数据库相关
本次试验中,数据库相关的操作主要是如何使用Python操作数据库,涉及的知识点不多。以下是一些简单的语句:
连接数据库
conn = MySQLdb.connect(host='localhost', user='root', passwd='mysql', db='test', charset='utf8')
值得注意的是最后一个参数的使用,否则从数据库中取出的数据可能会出现乱码。如果对乱码问题还有疑问,可以参考这篇文章:https://www.php.cn/link/b7799d0004c7a0b7c5e0672f7848ff04。
获取字段信息
fields = cursor.description
其中,
cursor
cursor.scroll(0, mode='absolute')
用于重置游标的位置。
获取数据
results = cursor.fetchall()
获取数据非常简单,但需要注意数据项是一个类似于二维数组的存在,在获取每个单元格项时应谨慎处理。
Excel基础
同样,这里讲解的是如何使用Python操作Excel数据。
工作薄(workbook)的概念是操作的基础,与下文的sheet相对应,workbook是sheet赖以生存的载体。
workbook = xlwt.Workbook()
所有的操作都在sheet上进行。
sheet = workbook.add_sheet('table_message', cell_overwrite_ok=True)如果对workbook和sheet的概念有些模糊,可以这样假设:workbook是整个Excel文件,而sheet是其中的一个工作表。
案例
下面是一个小案例。
# coding:utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
<h1><strong>author</strong> = '郭 璞'</h1><h1><strong>date</strong> = '2016/8/20'</h1><h1><strong>Desc</strong> = 从数据库中导出数据到excel数据表中</h1><p>import xlwt
import MySQLdb</p><p>conn = MySQLdb.connect('localhost', 'root', 'mysql', 'test', charset='utf8')
cursor = conn.cursor()
count = cursor.execute('select * from message')
print count</p><h1>重置游标的位置</h1><p>cursor.scroll(0, mode='absolute')</p><h1>搜取所有结果</h1><p>results = cursor.fetchall()</p><h1>获取MYSQL里面的数据字段名称</h1><p>fields = cursor.description</p><p>workbook = xlwt.Workbook()
sheet = workbook.add_sheet('table_message', cell_overwrite_ok=True)</p><h1>写上字段信息</h1><p>for field in range(0, len(fields)):
sheet.write(0, field, fields[field][0])</p><h1>获取并写入数据段信息</h1><p>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])</p><p>workbook.save(r'./readout.xlsx')封装
为了使用上的方便,现将其封装成一个容易调用的函数。
封装之后
# coding:utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')</p><h1><strong>author</strong> = '郭 璞'</h1><h1><strong>date</strong> = '2016/8/20'</h1><h1><strong>Desc</strong> = 从数据库中导出数据到excel数据表中</h1><p>import xlwt
import MySQLdb</p><p>def export(host, user, password, dbname, table_name, outputpath):
conn = MySQLdb.connect(host, user, password, dbname, charset='utf8')
cursor = conn.cursor()
count = cursor.execute('select * from ' + table_name)
print count</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;"># 重置游标的位置
cursor.scroll(0, mode='absolute')
# 搜取所有结果
results = cursor.fetchall()
# 获取MYSQL里面的数据字段名称
fields = cursor.description
workbook = xlwt.Workbook()
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])
# 获取并写入数据段信息
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(outputpath)if name == "main": export('localhost', 'root', 'mysql', 'test', 'datetest', r'datetest.xlsx')
测试结果
<pre class="brush:php;toolbar:false;">id name date 1 dlut 2016-07-06 2 清华大学 2016-07-03 3 北京大学 2016-07-28 4 Mark 2016-08-20 5 Tom 2016-08-19 6 Jane 2016-08-21
总结
回顾一下,本次试验用到了哪些知识点:
charset=utf-8
以上就是Python实现数据库一键导出为Excel表格!大大的增加了工作效率!的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号