用python执行sybase存储过程,将结果写入excel

#!/usr/bin/python

import Sybase
import xlwt
import types

if __name__ == '__main__':
    o = Sybase.connect('DBSVR', 'sa', '111111')
    c = o.cursor()
    c.execute('set chained off')
    c.callproc('imaptmdb..my_Test')
    result = c.fetchall()
    wb = xlwt.Workbook()
    ws = wb.add_sheet('Alarm')
    rowNum = 1 
    for row in result:
        colNum = 0
        #print rowNum
        for col in row:
            if type(col) != types.StringTypes:
                #print rowNum, colNum, col
                ws.write(rowNum, colNum, str(col))
            else:
                ws.write(rowNum, colNum, col)
            colNum = colNum + 1
        rowNum = rowNum + 1
    c.close()
    o.close()
    wb.save('emily.xls')
    print '----hello tiky'


安装sybase插件
>unzip pysybase.zip
>cp . sybasect.so /opt/python2.4/site-packages
>cp Sybase.py /opt/python2.4/site-packages

安装python写excel插件
>cd xlwt-0.7.2
>python setup.py install


你可能感兴趣的:(用python执行sybase存储过程,将结果写入excel)