需要帮助通过PowerShell模块“PSExcel”将工作表添加到现有的xlsx中

我正在创build一个项目,使用MSSQL数据库中的数据创build一些Excel报告,并且需要一些帮助来获得最终结果。

免责声明 – 我不擅长PowerShell和MSSQL)。

到目前为止,在互联网的帮助下,我设法创build了一个提升自身的.ps1文件,导入一个名为PSExcel的PS模块( https://github.com/RamblingCookieMonster/PSExcel ),导入SQLPS模块并运行三个单独的查询来导出到三个单独的.xlsx文件。

我最后几个部分的脚本是:

# Import PSExcel module Import-Module C:\scripts-and-reports\Modules\PSExcel # Import SQLPS module Import-Module sqlps # Import PSExcel module Import-Module C:\scripts-and-reports\Modules\PSExcel # Import SQLPS module Import-Module sqlps # Create individuals invoke-sqlcmd -inputfile "C:\scripts-and-reports\individuals.sql" -serverinstance "SERVER\INSTANCE" -database "DATABASE" | Export-XLSX -WorksheetName "Individuals" -Path "C:\scripts-and-reports\Individuals.xlsx" -Table -Force # Create joint parties invoke-sqlcmd -inputfile "C:\scripts-and-reports\joint-parties.sql" -serverinstance "SERVER\INSTANCE" -database "DATABASE" | Export-XLSX -WorksheetName "Joint Parties" -Path "C:\scripts-and-reports\Joint Parties.xlsx" -Table -Force # Create organisations invoke-sqlcmd -inputfile "C:\scripts-and-reports\organisations.sql" -serverinstance "SERVER\INSTANCE" -database "DATABASE" | Export-XLSX -WorksheetName "Organisations" -Path "C:\scripts-and-reports\Organisations.xlsx" -Table -Force 

我试图无效,结合最后两个查询导出到第一个查询的导出作为额外的工作表,所以我只有一个Excel工作簿交给我的老板,但我想我必须接近它不正确。

当我读取../Export-XLSX.ps1的 第94行的示例,并尝试通过更改文件名来相互匹配来实现它时,最后一个查询将replace输出的.xlsx文件中的第一个。 这一定是因为-Force 。 将其更改为-Append将无济于事,因为它说该文件已经存在。

任何人都可以帮助我,先给我看看我哪里出错,然后指着我在正确的方向(这可能最终在演练)。

请和谢谢!

**更新**

用@ gms0ulman对Export-XLSX.ps1的修复,看起来就像我已经用不同的SQL查询testing了我所需要的,并且它添加了工作表OK。 我用于testing的查询是

 SELECT DISTINCT case mt.[Status] when 0 then 'In Progress' When 1 then 'On Hold' when 2 then 'Completed' when 3 then 'Not Proceeding' else 'Unknown' end as MatterStatus, mt.LastUpdatedOn as LastModified from matter mt where mt.LastUpdatedOn >= '2016-07-01' AND (mt.[status] = 0 or mt.[status] = 1) 

虽然这(和我的PS脚本中的其他两个迭代)工作,我的实际查询不。 查询本身也工作,也是第一次导出,但是当-Append在PS中与invoke-sqlcmd and invoke-sqlcmd -inputfile "query2.sql" -serverinstance "" -database "" | Export-XLSX -WorksheetName "Joint Parties" -Path "C:\scripts-and-reports\Matter Details.xlsx" -Table -Append invoke-sqlcmd and invoke-sqlcmd -inputfile "query2.sql" -serverinstance "" -database "" | Export-XLSX -WorksheetName "Joint Parties" -Path "C:\scripts-and-reports\Matter Details.xlsx" -Table -Append ,两个附加的工作表出现错误:

使用“1”参数调用“SaveAs”的exception:“保存文件C:\ scripts-and-reports \ Matter Details.xlsx时出错”

在C:\ scripts-and-reports \ Modules \ PSExcel \ Export-XLSX.ps1:496 char:13

$ Excel.SaveAs($path)

~~~~~~~~~~~~~~~~~~~~

CategoryInfo:NotSpecified:(:) [],MethodInvocationException

FullyQualifiedErrorId:InvalidOperationException

问题出在您使用的模块上。 -Append应该工作。 在Export-XLSX.ps1文件中,查看第351行和第371行

当你提供一个现有的path, 351评估为真, 371永远不会被执行。 第371行是模块为您创build新工作表的位置。

 if (($Append -or $ClearSheet) -and ($PSBoundParameters.ContainsKey('Excel') -or (Test-Path $Path)) ) # line 351 { $WorkSheet=$Excel.Workbook.Worksheets | Where-Object {$_.Name -like $WorkSheetName} if($ClearSheet) { $WorkSheet.Cells[$WorkSheet.Dimension.Start.Row, $WorkSheet.Dimension.Start.Column, $WorkSheet.Dimension.End.Row, $WorkSheet.Dimension.End.Column].Clear() } if($Append) { $RealHeaderCount = $WorkSheet.Dimension.Columns if($Header.count -ne $RealHeaderCount) { $Excel.Dispose() Throw "Found $RealHeaderCount existing headers, provided data has $($Header.count)." } $RowIndex = 1 + $Worksheet.Dimension.Rows } } else { $WorkSheet = $Workbook.Worksheets.Add($WorkSheetName) #line 371 } 

为了解决这个问题,我添加了几行到Export-XLSX.ps1文件。 现在,即使path存在,它也会:

  • 检查是否提供了$ WorksheetName
  • 检查这个工作表不存在
  • 如果两个都是真的,则创build新的工作表。

请注意,您必须Remove-ModuleImport-Module才能识别更改。 您将需要使用-Append 。 我在第一张工作表上使用了-Force ,而不是第二张。 此外,您可能会发现-Verbose有帮助,因为它可以在脚本运行时为您提供更多信息 – 适用于debugging。

 if (($Append -or $ClearSheet) -and ($PSBoundParameters.ContainsKey('Excel') -or (Test-Path $Path)) ) { # New line: even if path exists, check for $WorkSheetName and that this worksheet doesn't exist if($WorkSheetName -and $Excel.Workbook.Worksheets | Where-Object {$_.Name -like $WorkSheetName} -eq $null) { # if you have $WorksheetName and it doesn't exist, create worksheet. $WorkSheet = $Workbook.Worksheets.Add($WorkSheetName) } else { $WorkSheet=$Excel.Workbook.Worksheets | Where-Object {$_.Name -like $WorkSheetName} if($ClearSheet) { $WorkSheet.Cells[$WorkSheet.Dimension.Start.Row, $WorkSheet.Dimension.Start.Column, $WorkSheet.Dimension.End.Row, $WorkSheet.Dimension.End.Column].Clear() } if($Append) { $RealHeaderCount = $WorkSheet.Dimension.Columns if($Header.count -ne $RealHeaderCount) { $Excel.Dispose() Throw "Found $RealHeaderCount existing headers, provided data has $($Header.count)." } $RowIndex = 1 + $Worksheet.Dimension.Rows } } # this is also new. } else { $WorkSheet = $Workbook.Worksheets.Add($WorkSheetName) }