python 利用win32com操作excel

#coding=cp936
import win32com
from adoconstants import *
from win32com.client import Dispatch,constants
import os
import pythoncom
class TranslateExcel:
    @staticmethod
    def ImportMssqlToExcel(ip,dbname,username,pwd,sql,filename):
        conn=Dispatch('ADODB.Connection')
        con_str="Provider=SQLOLEDB.1;Data Source=%s;Uid=%s;Pwd=%s;Database=%s;"%(ip,username,pwd,dbname)
        conn.ConnectionString=con_str
        conn.Open()
        rs=None
        book=None
        app=Dispatch('Excel.Application')
        app.Visible=0
        try:
            book=None
            if(os.path.isfile(filename)==True):
                book=app.Workbooks.Open(filename)
                sht=book.Sheets[0]
            else:
                book=app.Workbooks.Add()
                sht=book.Sheets.Add()
            #sht=book.Worksheets("Sheet1")
           
            cmd=Dispatch('ADODB.Command')
            cmd.acTiveConnection=conn
            cmd.CommandText=sql
            cmd.CommandType=adCmdText
            (rs,dummy)=cmd.Execute()
            row=0
            while not rs.EOF:
                i=0
                if(row==0):
                    while(i<rs.Fields.count):
                        sht.Cells(row+1,i+1).Value=rs(i).name
                        if(rs(i).value==None):
                            sht.Cells(row+2,i+1).Value=""
                        else:
                            sht.Cells(row+2,i+1).Value=rs(i).value
                        i=i+1
                else:
                    while i<rs.Fields.count:
                        if(rs(i).value==None):
                            sht.Cells(row+2,i+1).Value=""
                        else:
                            sht.Cells(row+2,i+1).Value=rs(i).value
                        i=i+1
                row=row+1
                print "执行成功%s"%str(rs.Fields('PRD_CODE').Value)
                rs.MoveNext()
                if(row==5):
                    break
        finally:
            #book.Save(filename)
            book.Close(SaveChanges=1)
            app.Quit()
            del app
            if(rs is not None):
                rs.Close()
            conn.Close()
pythoncom.CoInitialize()
#print "方法列表:"
#print "------ImportMssqlToExcel:将数据表数据导出生成Excel文件"
#ip=str(raw_input("请输入数据库IP地址:"))
#dbname=str(raw_input('请输入数据库名称:'))
#username=str(raw_input('请输入数据库用户名:'))
#pwd=str(raw_input('请输入数据库密码:'))
#sql=str(raw_input('请输入SQL:'))
#filename=str(raw_input('请输入文件名称:'))
#TranslateExcel.ImportMssqlToExcel(ip,dbname,username,pwd,sql,filename)
book= win32com.client.Dispatch('Word.Application')
#book=DispatchEx('Word.Application')
print "sdf"
book.Visible=0
#book.DisplayAlerts=0
#doc=book.Documents.Open(filename)
doc=book.Documents.Add()
myRange = doc.Range(0,0)
myRange.InsertBefore('Hello from Python!')
wordSel = myRange.Select()
#wordSel.Style = constants.wdStyleHeading1
table=doc.Tables.Add(myRange,3,4)
table.Cell(1,1).Range.InsertAfter('北京天安门')  # 新增文字到cell(1,1)
table.Cell(1,2).Range.InsertAfter('北京天安门1')
table.Cell(1,1).Range.Font.Name = "Arial"  # 設定字型為Arial
#table.Cell(1,1).Range.Font.Color = 0xFF0000  # 設定字型為blue
table.Rows.Add()     # 新增一個Row
table.Columns.Add()
#book.Close(SaveChanges=1)
book.Quit()
pythoncom.CoUninitialize()
print "执行成功..."

posted on 2010-03-22 11:56  reck for zhou  阅读(2301)  评论(0编辑  收藏  举报

导航