当前位置:   article > 正文

Python实现数据库一键导出为Excel表格

python提取数据库数据生成表格

数据库数据导出为excel表格,也可以说是一个很常用的功能了。毕竟不是任何人都懂数据库操作语句的。
下面先来看看完成的效果吧。

  • 数据源

数据源

  • 导出结果
    导出结果

依赖

由于是Python实现的,所以需要有Python环境的支持

Python2.7.11

我的Python环境是2.7.11。虽然你用的可能是3.5版本,但是思想是一致的。

xlwt

pip install xlwt

MySQLdb

pip install MySQLdb
如果上述方式不成功的话,可以到sourceforge官网上去下载windows上的msi版本或者使用源码自行编译。

数据库相关

本次试验,数据库相关的其实也就是如何使用Python操作数据库而已,知识点也很少,下述为我们本次用到的一些简单的语句。

连接

conn = MySQLdb.connect(host=’localhost’,user=’root’,passwd=’mysql’,db=’test’,charset=’utf8’)

这里值得我们一提的就是最后一个参数的使用,不然从数据库中取出的数据就会使乱码。关于乱码问题,如果还有不明白的地方,不妨看下这篇文章http://blog.csdn.net/marksinoberg/article/details/52254401

获取字段信息

fields = cursor.description

至于cursor,是我们操作数据库的核心。游标的特点就是一旦遍历过该条数据,便不可返回。但是我们也可以手动的改变其位置。

cursor.scroll(0,mode=’absolute’)来重置游标的位置

获取数据

获取数据简直更是轻而易举,但是我们必须在心里明白,数据项是一个类似于二维数组的存在。我们获取每一个cell项的时候应该注意。

results = cursor.fetchall()

Excel基础

同样,这里讲解的也是如何使用Python来操作excel数据。

workbook

工作薄的概念我们必须要明确,其是我们工作的基础。与下文的sheet相对应,workbook是sheet赖以生存的载体。

workbook = xlwt.Workbook()

sheet

我们所有的操作,都是在sheet上进行的。

sheet = workbook.add_sheet(‘table_message’,cell_overwrite_ok=True)

对于workbook 和sheet,如果对此有点模糊。不妨这样进行假设。

日常生活中记账的时候,我们都会有一个账本,这就是workbook。而我们记账则是记录在一张张的表格上面,这些表格就是我们看到的sheet。一个账本上可以有很多个表格,也可以只是一个表格。这样就很容易理解了吧。 :-)

案例

下面看一个小案例。

  1. # coding:utf8
  2. import sys
  3. reload(sys)
  4. sys.setdefaultencoding('utf8')
  5. # __author__ = '郭 璞'
  6. # __date__ = '2016/8/20'
  7. # __Desc__ = 从数据库中导出数据到excel数据表中
  8. import xlwt
  9. import MySQLdb
  10. conn = MySQLdb.connect('localhost','root','mysql','test',charset='utf8')
  11. cursor = conn.cursor()
  12. count = cursor.execute('select * from message')
  13. print count
  14. # 重置游标的位置
  15. cursor.scroll(0,mode='absolute')
  16. # 搜取所有结果
  17. results = cursor.fetchall()
  18. # 获取MYSQL里面的数据字段名称
  19. fields = cursor.description
  20. workbook = xlwt.Workbook()
  21. sheet = workbook.add_sheet('table_message',cell_overwrite_ok=True)
  22. # 写上字段信息
  23. for field in range(0,len(fields)):
  24. sheet.write(0,field,fields[field][0])
  25. # 获取并写入数据段信息
  26. row = 1
  27. col = 0
  28. for row in range(1,len(results)+1):
  29. for col in range(0,len(fields)):
  30. sheet.write(row,col,u'%s'%results[row-1][col])
  31. workbook.save(r'./readout.xlsx')
  • 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

封装

为了使用上的方便,现将其封装成一个容易调用的函数。

封装之后

  1. # coding:utf8
  2. import sys
  3. reload(sys)
  4. sys.setdefaultencoding('utf8')
  5. # __author__ = '郭 璞'
  6. # __date__ = '2016/8/20'
  7. # __Desc__ = 从数据库中导出数据到excel数据表中
  8. import xlwt
  9. import MySQLdb
  10. def export(host,user,password,dbname,table_name,outputpath):
  11. conn = MySQLdb.connect(host,user,password,dbname,charset='utf8')
  12. cursor = conn.cursor()
  13. count = cursor.execute('select * from '+table_name)
  14. print count
  15. # 重置游标的位置
  16. cursor.scroll(0,mode='absolute')
  17. # 搜取所有结果
  18. results = cursor.fetchall()
  19. # 获取MYSQL里面的数据字段名称
  20. fields = cursor.description
  21. workbook = xlwt.Workbook()
  22. sheet = workbook.add_sheet('table_'+table_name,cell_overwrite_ok=True)
  23. # 写上字段信息
  24. for field in range(0,len(fields)):
  25. sheet.write(0,field,fields[field][0])
  26. # 获取并写入数据段信息
  27. row = 1
  28. col = 0
  29. for row in range(1,len(results)+1):
  30. for col in range(0,len(fields)):
  31. sheet.write(row,col,u'%s'%results[row-1][col])
  32. workbook.save(outputpath)
  33. # 结果测试
  34. if __name__ == "__main__":
  35. export('localhost','root','mysql','test','datetest',r'datetest.xlsx')
  • 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

测试结果

  1. id name date
  2. 1 dlut 2016-07-06
  3. 2 清华大学 2016-07-03
  4. 3 北京大学 2016-07-28
  5. 4 Mark 2016-08-20
  6. 5 Tom 2016-08-19
  7. 6 Jane 2016-08-21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

总结

回顾一下,本次试验用到了哪些知识点。

  • Python简易操作数据库
  • Python简易操作Excel
  • 数据库取出数据乱码问题解决之添加charset=utf-8
  • 以二维数组的角度来处理获取到的结果集。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/967441
推荐阅读
相关标签
  

闽ICP备14008679号