我使用powershell导出SQL服务器的结果(使用汇总功能),但是收到以下错误。
物业' NumberFormat'在这个对象上找不到。验证该属性是否存在且可以设置。 在行:79 char:1 + $ xlsSh.Cells.NumberFormat =" @&#34 ;; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:PropertyNotFound
您无法在空值表达式上调用方法。
SQL查询
SELECT COALESCE(d.Region,'Total') AS REGION,
COALESCE([financialBudgetLineDesc],'') AS FINANCIALBUDGETLINEDESC,
COALESCE([budgetLineDescription],'') AS BUDGETLINEDESC,
([BudgetAllocation]) AS 'Budget Annual MD',
ISNULL(a.recordNumber,'') AS CR,COALESCE(a.Title,'') AS Title
, ISNULL(SUM(CASE WHEN Period = '2018/01' THEN [Time Spent] ELSE NULL END), 0) AS [JAN Actual MD Spent]
, ISNULL(SUM(CASE WHEN Period = '2018/02' THEN [Time Spent] ELSE NULL END),0) AS [FEB Actual MD Spent]
, ISNULL(SUM(CASE WHEN Period = '2018/03' THEN [Time Spent] ELSE NULL END),0) AS [MAR Actual MD Spent]
, ISNULL(SUM(CASE WHEN Period = '2018/04' THEN [Time Spent] ELSE NULL END),0) AS [APR Actual MD Spent]
, ISNULL(SUM(CASE WHEN Period = '2018/05' THEN [Time Spent] ELSE NULL END),0) AS [MAY Actual MD Spent]
,ISNULL(SUM([Time Spent]),0) AS [YTD Actual MD Spent]
FROM [dbo].[combinedBespokeCPsandCRs] a
LEFT JOIN [dbo].[CompleteData] b ON a.recordNumber=RIGHT(b.[ABC Time Reporting CR],8)
JOIN [ARIDashboard].dbo.Account c ON a.accountId=c.AccountID
JOIN [ARIDashboard].dbo.Region d ON c.RegionID=d.RegionID
JOIN dbo.budgetLineMapping e ON e.regionId=d.RegionID
JOIN dbo.budgetLine f ON a.budgetLineId=f.budgetLineId
JOIN dbo.financialBudgetLine g ON e.financialBudgetLineId=g.financialBudgetLineId
GROUP BY ROLLUP (d.Region,[financialBudgetLineDesc],([budgetLineDescription],[BudgetAllocation]),(a.recordNumber,a.Title))
ORDER BY d.Region,[financialBudgetLineDesc],[budgetLineDescription],a.Title
Power Shell
## - Connect to SQL Server using non-SMO class 'System.Data':
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = `
"Server = $SQLServer; Database = $Database; Integrated Security = True";
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandText = $SqlQuery;
$SqlCmd.Connection = $SqlConnection;
## - Extract and build the SQL data object '$DataSetTable':
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$SqlAdapter.SelectCommand = $SqlCmd;
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet);
$DataSetTable = $DataSet.Tables["Table"];
## ---------- Working with Excel ---------- ##
## - Create an Excel Application instance:
$xlsObj = New-Object -ComObject Excel.Application;
## - Create new Workbook and Sheet (Visible = 1 / 0 not visible)
$xlsObj.Visible = 0;
$xlsWb = $xlsobj.Workbooks.Add();
$xlsSh = $xlsWb.Worksheets.item(1);
## - Build the Excel column heading:
[Array] $getColumnNames = $DataSetTable.Columns | Select ColumnName;
## - Build column header:
[Int] $RowHeader = 1;
foreach ($ColH in $getColumnNames)
{
if(!($getColumnNames))
{
$xlsSh.Cells.item(1, $RowHeader).font.bold = $true;
$xlsSh.Cells.item(1, $RowHeader) = $ColH.ColumnName;
$RowHeader++;
}
};
## - Adding the data start in row 2 column 1:
[Int] $rowData = 2;
[Int] $colData = 1;
foreach ($rec in $DataSetTable.Rows)
{
foreach ($Coln in $getColumnNames)
{
## - Next line convert cell to be text only:
$xlsSh.Cells.NumberFormat = "@";
## - Populating columns:
**if(!($rec)){
$xlsSh.Cells.Item($rowData, $colData) = ` $rec.$($Coln.ColumnName).ToString();
}
$ColData++;
};**
$rowData++; $ColData = 1;
};
## - Adjusting columns in the Excel sheet:
$xlsRng = $xlsSH.usedRange;
$xlsRng.EntireColumn.AutoFit();
## ---------- Saving file and Terminating Excel Application ---------- ##
## - Saving Excel file - if the file exist do delete then save
$xlsFile = `
"C:\Temp\NewExceldbResults_$((Get-Date).ToString("yyyyMMdd_hhmmss")).xls";
if (Test-Path $xlsFile)
{
Remove-Item $xlsFile
$xlsObj.ActiveWorkbook.SaveAs($xlsFile);
}
else
{
$xlsObj.ActiveWorkbook.SaveAs($xlsFile);
};
## Quit Excel and Terminate Excel Application process:
$xlsObj.Quit(); (Get-Process Excel*) | foreach ($_) { $_.kill() };
## - End of Script - ##
任何建议?