2017-01-26 7 views
0

Я хочу импортировать файл .xlsx в powershell, и для этого я узнал, что командлет - Import-Excel или Import-Xlsx) и т. Д. Но эти ошибки при попытке запустить их в powershell 4.0, командлет не найден.Модуль Import-Excel не работает

Как ими пользоваться? Также Install-Module не работает в Powershell 4.0?

+0

На [https://github.com/dfinke/ImportExcel](https://github.com/dfinke/ImportExcel) прокрутите вниз до пункта «Установка» и прочтите инструкции. Найдите PowerSellGet или [Модули PowerShell модуля PackageManagement] (https://www.microsoft.com/en-us/download/details.aspx?id=51451) для последних – LotPings

ответ

0

Ниже сервера Функции Powershell цель

function Import-Excel 

{ 

    [CmdletBinding(SupportsShouldProcess=$true)] 

    Param( 
     [parameter( 
      mandatory=$true, 
      position=1, 
      ValueFromPipeline=$true, 
      ValueFromPipelineByPropertyName=$true)] 
     [String[]] 
     $Path, 

     [parameter(mandatory=$false)] 
     $Worksheet = 1, 

     [parameter(mandatory=$false)] 
     [switch] 
     $Force 
    ) 

    Begin 
    { 
     function GetTempFileName($extension) 
     { 
      $temp = [io.path]::GetTempFileName(); 
      $params = @{ 
       Path = $temp; 
       Destination = $temp + $extension; 
       Confirm = $false; 
       Verbose = $VerbosePreference; 
      } 
      Move-Item @params; 
      $temp += $extension; 
      return $temp; 
     } 

     # since an extension like .xls can have multiple formats, this 
     # will need to be changed 
     # 
     $xlFileFormats = @{ 
      # single worksheet formats 
      '.csv' = 6;  # 6, 22, 23, 24 
      '.dbf' = 11;  # 7, 8, 11 
      '.dif' = 9;  # 
      '.prn' = 36;  # 
      '.slk' = 2;  # 2, 10 
      '.wk1' = 31;  # 5, 30, 31 
      '.wk3' = 32;  # 15, 32 
      '.wk4' = 38;  # 
      '.wks' = 4;  # 
      '.xlw' = 35;  # 

      # multiple worksheet formats 
      '.xls' = -4143; # -4143, 1, 16, 18, 29, 33, 39, 43 
      '.xlsb' = 50;  # 
      '.xlsm' = 52;  # 
      '.xlsx' = 51;  # 
      '.xml' = 46;  # 
      '.ods' = 60;  # 
     } 

     $xl = New-Object -ComObject Excel.Application; 
     $xl.DisplayAlerts = $false; 
     $xl.Visible = $false; 
    } 

    Process 
    { 
     $Path | ForEach-Object { 

      if ($Force -or $psCmdlet.ShouldProcess($_)) { 

       $fileExist = Test-Path $_ 

       if (-not $fileExist) { 
        Write-Error "Error: $_ does not exist" -Category ResourceUnavailable;    
       } else { 
        # create temporary .csv file from excel file and import .csv 
        # 
        $_ = (Resolve-Path $_).toString(); 
        $wb = $xl.Workbooks.Add($_); 
        if ($?) { 
         $csvTemp = GetTempFileName(".csv"); 
         $ws = $wb.Worksheets.Item($Worksheet); 
         $ws.SaveAs($csvTemp, $xlFileFormats[".csv"]); 
         $wb.Close($false); 
         Remove-Variable -Name ('ws', 'wb') -Confirm:$false; 
         Import-Csv $csvTemp; 
         Remove-Item $csvTemp -Confirm:$false -Verbose:$VerbosePreference; 
        } 
       } 
      } 
     } 
    } 

    End 
    { 
     $xl.Quit(); 
     Remove-Variable -name xl -Confirm:$false; 
     [gc]::Collect(); 
    } 
} 

Надеется, что это помогает!

Смежные вопросы