这个库用法与Python完全一致,随心更新。
; Author: Mono
; Time: 2022.08.09
; Version: 0.0.1
; **注意所有文件路径均为完整路径,若为不完整路径
; **则优先访问我的文档
Class Xlwings
{
Class App
{
__New(visible := True, add_book := False)
{
this.app := ComObject("Excel.Application") ; handle
this.app.Visible := visible ; by default excel sheets are invisible
if add_book
this.book := this.app.Workbooks.Add()
this.books := Xlwings.app.book(this.app)
this.DefineProp("display_alerts", {Get : DAGet, Set : DASet})
DAGet(_)
{
Return this.app.DisplayAlerts
}
DASet(_, Value)
{
this.app.DisplayAlerts := Value
}
}
Quit()
{
this.app.quit()
}
Range(item)
{
Return this.app.Range(item)
}
Class Book
{
__New(app)
{
this.app := app
}
Close(Sign := True, Url := "")
{
if !Url
this.wb.Close(Sign)
else
{
Try
this.wb.Close(Sign, Url)
Catch
this.wb.Close(Sign, A_ScriptDir "\" Url)
}
}
Open(Url)
{
Try
this.wb := this.app.Workbooks.open(Url)
Catch
this.wb := this.app.Workbooks.open(A_ScriptDir "\" Url)
this.Name := this.wb.Name
this.FullName := this.wb.FullName
this.sheets := Xlwings.app.book.sheet(this.wb)
Return this
}
Save(Url := "")
{
if !Url
Return this.wb.save()
else
Return this.Saveas(Url)
}
Saveas(Url)
{
Try
Return this.wb.saveas(Url)
Catch
Return this.wb.saveas(A_ScriptDir "\" Url)
}
Class Sheet
{
__New(wb)
{
this.wb := wb
}
__Item[Index]
{
Get => this.GetFunc(Index)
Set => ""
}
GetFunc(Index)
{
this.sheet := this.wb.sheets[Index]
Return this
}
Range(Index)
{
Return this.sheet.Range(Index)
}
}
}
}
}
; 测试
app := xlwings.app()
wb := app.books.open("C:\Users\HP\Desktop\MST优化时代.xlsx")
; ws := wb.sheets[1]
ws := wb.sheets["Sheet1"]
app.display_alerts := 1
; ws.range("A4").value := 5
ws.range("A4:A7")[0].value := 5
; msgbox ws.Range("A4:A7")[2].Value
wb.save()
wb.close()
app.quit()
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
?
赞一个
牛逼plus啊
这个需要安装excel