前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python实现mysql中数据导成excel表格

Python实现mysql中数据导成excel表格

作者头像
WindCoder
发布于 2018-09-20 08:17:54
发布于 2018-09-20 08:17:54
2.5K00
代码可运行
举报
文章被收录于专栏:WindCoderWindCoder
运行总次数:0
代码可运行

V1.0

1、支持中文数据转换,此处数据库等信息均使用utf-8。

2、日期格式可自定义(对于东代码的朋友来说),默认格式:2015-04-21 20:31:21

3、目前仅实现了创建新表功能,同一表名会覆盖原表。

4、目前仅在windows平台测试过,linux平台可能需要修改下编码。

Python代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Create your views here.
# -*- coding: utf-8 -*-
import MySQLdb
import xlwt
from datetime import datetime

wbk=xlwt.Workbook(encoding='utf-8')
sheet=wbk.add_sheet('sheet 2')
sheet.write(0,0,'Uid')
sheet.write(0,1,u'姓名')
sheet.write(0,2,'password')
sheet.write(0,3,'Email')
sheet.write(0,4,'Date')
row=1
fmt='YYYY-MM-DD hh:mm:ss'
try:
    conn=MySQLdb.connect(host="localhost",user='root',passwd="",db="personalblog_db",charset='utf8')
    cur=conn.cursor()
    count=cur.execute('select * from users')
    print 'there has %s rows record' % count
    results=cur.fetchmany(count)
    for uid,uname,upassword,uemail,udate in results:
        sheet.write(row,0,uid)
        sheet.write(row,1,uname)
        sheet.write(row,2,upassword)
        sheet.write(row,3,uemail)
        style = xlwt.XFStyle()
        style.num_format_str = fmt
        sheet.write(row,4,udate,style)
        row+=1
    wbk.save('users1.xls')
    cur.close()
    conn.commit()
    conn.close()
except MySQLdb.Error,e:
    print "Mysql Error %d: %s"%(e.args[0],e.args[1])
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-04-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • V1.0
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档