Adding Excel Sheets to End of Workbook

瘦欲@ 提交于 2019-12-20 04:09:12

问题


I am trying to add excel worksheets to the end of a workbook, reserving the first sheet for a summary.

import win32com.client

Excel = win32com.client.DispatchEx('Excel.Application')
Book = Excel.Workbooks.Add()

Excel.Visible = True

Book.Worksheets(3).Delete()
Book.Worksheets(2).Delete()

Sheet = Book.Worksheets(1)

Sheet.Name = "Summary"

Book.Worksheets.Add(After=Sheet)

Sheet = Book.Worksheets(2)

Sheet.Name = "Data1"

This code adds the new sheet to the left, despite using After=Sheet, and when I modify the sheet named "Data1", it overwrites the sheet named "Summary".

This is similar to this problem:

Adding sheets to end of workbook in Excel (normal method not working?)

but the given solutions don't work for me.


回答1:


Try using this by adding Before = None:

add = Book.Sheets.Add(Before = None , After = Book.Sheets(book.Sheets.count))
add.Name = "Data1"



回答2:


Try using Sheet = excelApp.ActiveSheet:

Book.Worksheets.Add(After=Sheet)
Sheet = Book.ActiveSheet
Sheet.Name = "Data1"


来源:https://stackoverflow.com/questions/40179804/adding-excel-sheets-to-end-of-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!