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 "執行成功..."

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章