从c#运行PowerShell脚本时Excel不更新

时间:2017-12-13 14:23:45

标签: c# powershell

我有以下PowerShell脚本,我试图从c#运行。

脚本接受从C#代码传递的两个参数,并正确加载PowerShell脚本。

param([string]$csvFile, [string]$xlpath)
$processes = Import-Csv -Path $csvFile -header "LIMSINVOICENR","JOBID","CUSTOMERID","POREFERENCE","CURRENCY","ITEM","DETAILS","QTY","UNITPRICE"
$Excel = New-Object -ComObject excel.application 
$Excel.visible = $false 
$workbook = $Excel.Workbooks.Open($xlpath)
$workbook.Sheets.Item("SalesOrders").Activate() | Out-Null

$i = 5 
$unit = $excel.cells.item($i,14).Text
foreach($process in $processes) first line of the CSV file
{ 
    $excel.cells.item($i,4) = $process.LIMSINVOICENR
    $excel.cells.item($i,5) = $process.JOBID
    $excel.cells.item($i,6) = $process.CUSTOMERID
    $excel.cells.item($i,7) = $process.POREFERENCE
    $excel.cells.item($i,8) = $process.CURRENCY
    $excel.cells.item($i,9) = $process.ITEM
    $excel.cells.item($i,10) = $process.DETAILS
    $excel.cells.item($i,11) = $process.QTY
    $excel.cells.item($i,12) = $process.UNITPRICE

    $excel.cells.item($i,14) = $unit
    $i++ 
}

$workbook.Save() | Out-Null
$workbook.Close() | Out-Null
$Excel.Quit() 

Remove-Variable -Name excel 
[gc]::collect() 
[gc]::WaitForPendingFinalizers()

我正在尝试使用System.Management.Automation库,使用以下命令:

    public void UpdateWorksheet(string FullCsvFilePath, string FullWorksheetFilePath)
    {
        String powershellScript = AppDomain.CurrentDomain.BaseDirectory + "UpdateToAX.PS1";

         using (PowerShell PowerShellInstance = PowerShell.Create())
        {

            PowerShellInstance.AddCommand(powershellScript);
            PowerShellInstance.AddParameter("csvFile", FullCsvFilePath);
            PowerShellInstance.AddParameter("xlpath", FullWorksheetFilePath);
            PowerShellInstance.Invoke();
        }
    }

运行脚本并正在读取CSV文件,但看起来Excel文件未被读取或写入。当没有从C#程序调用时,脚本独立工作。

以下内容在UpdateWorksheet进程之前运行:

    public void OnCreated(object source, FileSystemEventArgs e)
    {
        this._MessageLog.SaveToLogFile("File: " + e.FullPath + " " + e.ChangeType);

        try
        {
            String FullWorksheetName = SystemWriter.CreateWorksheet(XMLSettings.TemplateFileName, e.Name.Remove(e.Name.Length -4, 4), XMLSettings.OutputFilePath);
            SystemWriter.UpdateWorksheet(e.FullPath, FullWorksheetName);
            this._MessageLog.SaveToLogFile("File: " + e.FullPath + " Uploaded to " + FullWorksheetName);
        }
        catch (Exception exception)
        {
            this._MessageLog.SaveToLogFile("Error Updating the '" + e.Name + "' Worksheet : " + exception.Message);
        }
    }

    public String CreateWorksheet(string TemplateFileFullName, string CsvFileName, string WorkingDirectoryPath)
    {
        string WorkFileName = CsvFileName + ".xlsx";

        //copy and rename the template file to the working directory
        File.Copy(TemplateFileFullName, WorkingDirectoryPath + WorkFileName);

        return WorkingDirectoryPath + WorkFileName;
    }

0 个答案:

没有答案