Python爬取sql server 2008数据

啥也是不说!!!!直接干代码!!!

这个版本的代码只支持数据为65536条以下的

# coding:utf8
import sys

import xlwt
import pyodbc
import os

import row

connect = pyodbc.connect(r'DRIVER={SQL Server Native Client 10.0};'r'SERVER=(local);'r'DATABASE=zilaishui;UID=sa;PWD=123456')
cursor = connect.cursor()
count = cursor.execute('SELECT TOP 100 [f_ID],[OrderNo],[d_Name],[d_Start_Page],[d_End_Page],[d_InStore],[File_Address],[d_Remark],[File_Dir] FROM [zilaishui].[dbo].[Detail_Pipe]')
row = cursor.fetchall()  # sql语句执行结果的获取,如果需要一次获取多条记录,可以使用cursor.fetchall()方法
# if row:
# print (row)

connect.commit()
connect.close()

def set_style(name, height, bold=False):
    style = xlwt.XFStyle()  # 初始化样式
    font = xlwt.Font()  # 为样式创建字体
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style


def write_excel():
    # 创建工作簿
    workbook = xlwt.Workbook(encoding='utf-8')
    # 创建sheet
    data_sheet = workbook.add_sheet('201903', cell_overwrite_ok=True)
    # 列表格式数据
    excelData = row
    # 定义循环下标
    index = 0

    file_style = xlwt.XFStyle()
    for i in excelData:
        # 每一列的内容(i)
        for x, item in enumerate(i):
            # 下标(x),单元元素(item)
            #  data_sheet.write(index, x, item, set_style('Times New Roman',220, True))
            data_sheet.write(index, x, item, file_style)
        index += 1
        # sys.exit();
        # 保存文件
    workbook.save('PJDM.xlsx')
    print(os.getcwd())


if __name__ == '__main__':
    write_excel()
posted @ 2020-12-18 15:17  Rosemajor  阅读(180)  评论(0编辑  收藏  举报