简体   繁体   English

如何将Excel工作簿合并到一个工作簿中并命名工作表

[英]How to Combine Excel Workbooks into One Workbook and Name the Worksheets

I am trying to combine several Excel workbooks into a single workbook while naming the worksheets the file name of the copied workbook. 我正在尝试将多个Excel工作簿合并到一个工作簿中,同时将工作表命名为复制的工作簿的文件名。

I've tried moving $Worksheet.Name within the for loop and outside. 我试过在for循环和外部移动$ Worksheet.Name。 It still only gives a name to the empty worksheet in the new workbook - naming it the last file copied. 它仍然只为新工作簿中的空工作表指定一个名称 - 将其命名为复制的最后一个文件。

$ExcelObject=New-Object -ComObject excel.application
$ExcelObject.visible=$true
$ExcelFiles=Get-ChildItem -Path C:\Users\u1\Documents\Testing

$Workbook=$ExcelObject.Workbooks.add()
$Worksheet=$Workbook.Sheets.Item("Sheet1")

foreach($ExcelFile in $ExcelFiles){

$Everyexcel=$ExcelObject.Workbooks.Open($ExcelFile.FullName)
$Everysheet=$Everyexcel.sheets.item(1)
$Everysheet.Copy($Worksheet)
$Worksheet.Name = [io.path]::GetFileNameWithoutExtension($ExcelFile) 
$Everyexcel.Close()
}

$Workbook.SaveAs("C:\Users\u1\Documents\Testing\New\merged.xlsx")
$ExcelObject.Quit()

The new workbook is created (merged.xlsx) but the worksheets are all named Sheet1(...) except for one empty worksheet that is named the last file in the directory. 创建了新工作簿(merged.xlsx),但工作表全部命名为Sheet1(...),但一个空工作表被命名为目录中的最后一个文件。 I also get the following errors. 我也得到以下错误。 Excel cannot access 'New'. Excel无法访问“新建”。 The document may be read-only or encrypted. 该文档可以是只读的或加密的。 You cannot call a method on a null-valued expression. 您无法在空值表达式上调用方法。

The problem you're facing is that the $Everysheet.Copy($Worksheet) call is copying both the values and the sheet names from $Everysheet and placing it before $Worksheet . 您遇到的问题是$Everysheet.Copy($Worksheet)调用正在复制$Everysheet的值和工作表名称并将其放在$Worksheet之前。 You can see the documentation of the Copy method here . 您可以在此处查看Copy方法的文档。

I'm unsure of how you'd track the sheet names to the individual workbooks in order to rename them, however I highly recommend using the ImportExcel module for any Excel-related files. 我不确定如何跟踪单个工作簿的工作表名称以重命名它们,但我强烈建议将ImportExcel模块用于任何与Excel相关的文件。 It makes them much easier to work with and only takes a couple seconds to get it working. 它使它们更容易使用,只需几秒钟就可以使它工作。 Moreover it has the benefit of using reading and editing Excel files without needing Microsoft Excel to be installed. 此外,它还具有使用读取和编辑Excel文件而无需安装Microsoft Excel的好处。

You can accomplish what you're looking to do in a few lines: 您可以通过几行完成您想要完成的任务:

$exportExcelPath = "C:\Users\u1\Documents\Testing\New\merged.xlsx"

$excelFiles = Get-ChildItem -Path 'C:\Users\u1\Documents\Testing' -Filter "*.xlsx"
foreach ($excelFile in $excelFiles)
{
    Import-Excel -Path $excelFile.FullName | Export-Excel -Path $exportExcelPath -WorkSheetname ([io.path]::GetFileNameWithoutExtension($excelFile))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,如何将不同的 excel 工作簿合并为一个 excel 工作簿作为工作簿 - Python, how to combine different excel workbooks into one excel workbook as sheets 将多个工作簿合并到一个 Excel 工作簿中 - combine multiple workbooks in one excel-workbook Excel VBA:将多个工作簿合并为一个工作簿 - Excel VBA: Combine multiple workbooks into one workbook 将具有特定名称的Excel工作表从多个工作簿复制到新工作簿 - Copy Excel Worksheets with Specific Name from Multiple Workbooks to New Workbook 如何将40个工作簿中的Excel数据与每个工作簿中的25个工作表合并? - How to combine Excel data from 40 Workbooks with 25 Worksheets in each? 有没有办法在python中将Excel工作簿组合成一个主工作簿? - Is there a way to combine Excel workbooks into one Master workbook within python? VBA:将多个工作簿(带有多个工作表)合并到一个工作簿中,数据一个在另一个下面 - VBA : Combine multiple Workbooks(with mutiple Worksheets) into One Workbook with Data One Below The Other 将多个 Excel 工作簿合并为一个包含多张工作表的工作簿 - Combine Multiple Excel Workbooks into one Workbook with multiple sheets 将多个工作簿合并为一个工作簿 - Combine multiple workbooks to one workbook 将文件夹中的多个工作簿合并到一个文件中,每个工作簿作为单独的工作表,文件名 = 工作表名 - Excel VBA 宏 - Combine multiple workbooks in a folder into one file, every workbook as a separate sheet, file name = sheet name - Excel VBA macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM