将Excel工作表添加到工作簿末尾 [英] Adding Excel Sheets to End of Workbook

查看:227
本文介绍了将Excel工作表添加到工作簿末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将excel工作表添加到工作簿的末尾,保留第一张工作表以进行摘要.

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"

尽管使用After=Sheet,此代码仍将新工作表添加到左侧,当我修改名为"Data1"的工作表时,它将覆盖名为"Summary"的工作表.

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".

这类似于此问题:

在工作表末添加工作表Excel中的工作簿(正常方法不起作用?)

但是给定的解决方案对我不起作用.

but the given solutions don't work for me.

推荐答案

尝试通过添加Before = None来使用它:

Try using this by adding Before = None:

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

这篇关于将Excel工作表添加到工作簿末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆