print("从《Python 核心编程》学习Microsoft Office编程");Step1(Excel);

我从未见过如此简单的Excel控制程序

简述

你们可能见过我的另一篇关于Excel控制的文章:
《使用VC++6.0 MFC OLE操作Excel 进行简单的读写》
http://blog.csdn.net/llwodao/article/details/51568472

申请对象,载入资源,free对象,简直是一场灾难QAQ.
然后python来了,突然感觉写入就像是简单的赋值。
#EXCEL Control Demo
from tkinter import Tk
from time import sleep
from tkinter.messagebox import showwarning
import win32com.client as win32

warn = lambda app: showwarning(app , 'Exit?')
RANGE = range(3 ,8);

def excel():
    app = 'Excel'
    x1 = win32.gencache.EnsureDispatch('%s.Application'%app)
    ss = x1.Workbooks.Add()
    sh = ss.ActiveSheet
    x1.Visible = True
    sleep(1)

    sh.Cells(1,1).Value = 'Python-to-%s Demo' %app
    sleep(1)
    for i in RANGE:
        sh.Cells(i,1).Value = 'Line %d' %i
        sleep(1)
    sh.Cells(i+2,1).Value = "It comes to the end!"
    warn(app)
    ss.Close(False)
    x1.Application.Quit()

if __name__ =='__main__':
    Tk().withdraw()
    excel()

猜你喜欢

转载自blog.csdn.net/llwodao/article/details/52767355