#coding=utf-8
import sys
import xlwt
import pymysql as MySQLdb #这里是python3 如果你是python2.x的话,import MySQLdb
import datetime

host = '192.168.10.109'
user = 'root'
pwd = ''
port = 3306
db = 'com66nao_mi'
sheet_name = 'report'
out_path = r'D:\SQL\aaa'+'.xls'
print(out_path)
sql = '''select * from mi_orgs;'''

def export():
conn = MySQLdb.connect(host,user,pwd,db,charset='utf8') 
cursor = conn.cursor() 
count = cursor.execute(sql) 
print("查询出" + str(count) + "条记录")

#来重置游标的位置 
cursor.scroll(0,mode='absolute')
#搜取所有结果 
results = cursor.fetchall()

# 获取MYSQL里面的数据字段名称
fields = cursor.description
workbook = xlwt.Workbook() # workbook是sheet赖以生存的载体。 
sheet = workbook.add_sheet(sheet_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(out_path)

#结果测试
if __name__=="__main__":
export()

python实现查询的数据写入到excel的更多相关文章

  1. 用python在后端将数据写入到数据库并读取

    用python在后端将数据写入到数据库: # coding:utf- import pandas as pd from sqlalchemy import create_engine # 初始化数据库 ...

  2. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  3. 初识python: xlsxwriter 将数据写入Excel

    使用 xlsxwriter 模块将数据写入excel . #!/user/bin env python # author:Simple-Sir # time:2020/9/24 12:51 # 使用 ...

  4. 用python包xlwt将数据写入Excel中

    一般用两种格式的数据写入,不多说放上demo. 1.列表形式写入 import xlwt def data_write(file_path, datas): f = xlwt.Workbook() s ...

  5. jsp页面查询的数据导出到excel

    java导入导出excel操作(jxl) jxl.jar 包下载地址:http://www.andykhan.com/jexcelapi/真实下载地址:http://www.andykhan.com/ ...

  6. 将从数据库中获取的数据写入到Excel表中

    pom.xml文件写入代码,maven自动加载poi-3.1-beta2.jar <!-- https://mvnrepository.com/artifact/poi/poi --> & ...

  7. C#使用NPOI将DataGridView内数据写入电子表格Excel

    NPOI能够在用户没有安装office的情况下读写office文件,包括.xls/.doc/.ppt等类型的文件.本文介绍的是使用NPOI库内的函数读写Excel(.xls)内的内容.在使用NPOI之 ...

  8. Python将MySQL表数据写入excel

    背景:将mysql表查询结果写入excel. 1.使用sqlyog工具将查询结果导出到Excel.xml中,用excel打开发现:因为text字段中有回车换行操作,显示结果行是乱的. 2.用mysql ...

  9. Python 把数据库的数据导出到excel表

    import io,xlwt def export_excel(request): """导出数据到excel表""" list_obj = ...

随机推荐

  1. Kylin存储和查询的分片问题

    本文来自网易云社区 作者:汪胜 相关概念介绍 为了了解Kylin存储和查询的分片问题,需要先介绍两个重要概念:segment和cuboid.相信大数据行业的相关同学都不陌生.Kylin每次提交一个新的 ...

  2. Docker 的部署方式

    在使用 docker run 命令启动 Docker 容器时,如果需要进行端口映射.目录挂载.网络信息等配置,整条命令将变得非常长,并且由于是一条 shell 命令,修改和复用也不方便.我们在大规模部 ...

  3. 多线程《八》线程queue

    一 线程queue queue is especially useful in threaded programming when information must be exchanged safe ...

  4. kali linux之本地提权

    已实现本地低权限帐号登录,希望获取更高权限,实现对目标进一步控制 系统帐号之间权限隔离(操作系统安全的基础,用户空间,内核空间) 系统帐号(用户帐号登录时获取权限令牌,服务帐号无需用户登录已在后台启动 ...

  5. Squid系统服务脚本

    #!/bin/bash # chkconfig: - 90 25 #其中-的意思是所有运行级别 # config: /etc/squid.conf # pidfile: /usr/local/squi ...

  6. win10+anaconda环境下pyqt5+qt tools+eric6.18安装及汉化过程

    最近需要用python编写一个小程序的界面,选择了pyqt5+eric6的配套组合,安装过程中遇到一些坑,特此记录.参考书籍是电子工业出版社的<PyQt5快速开发与实战>. 因为我使用an ...

  7. SDUT OJ 数据结构实验之链表一:顺序建立链表

    数据结构实验之链表一:顺序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. 将form转为ajax提交的js代码

    参考网络代码基础上进行修改,调试通过. 在html中插入下面的代码: 函数ajaxSubmit是submit的ajax形式. 注意:这里面使用到了jquery库 //<!--将form中的值转换 ...

  9. gym 102082B dp

    和51nod1055 一样: #include<iostream> #include<cstdio> #include<algorithm> #include< ...

  10. Java getMethod类型参数

    public class DynamicInvoker { public static void main(String[] args) { // TODO Auto-generated method ...