Finding Powershell names for Excel features

后端 未结 2 1024
小蘑菇
小蘑菇 2021-01-28 01:47

I want to automate the Excel process of Data → Import Text File → text import wizard

How do I find the PowerShell name for these menus?

Here i

2条回答
  •  旧巷少年郎
    2021-01-28 02:14

    If I understood your question, you are looking at a way to import a CSV file directly into Excel.

    I use the Open() method of the $excel.workbooks object.

    $excel = new-object -comobject excel.application
    $file = get-item "d:\scripts\test.csv"
    $excel.Visible=$true
    $excel.displayalerts = $False
    $wb = $excel.workbooks.open($file)
    

    If that doesn't work, I use files I rename the .CSV file to .TXT, and open them with OpenText

    UPDATED to use '|' as delimiter

    $wb = $excel.Workbooks.OpenText(
             "mycsv.txt", # file to open
             [Microsoft.Office.Interop.Excel.XlPlatform]::xlWindows, 
             1, # start from row 1
             [Microsoft.Office.Interop.Excel.XlTextParsingType]::xlDelimited,
             [Microsoft.Office.Interop.Excel.XlTextQualifier]::xlTextQualifierDoubleQuote,
             $false, # Consecutive Delimiter
             $false, # tab
             $false, # semicolon
             $false, # comma
             $false, # space
             $true,  # use other
             '|')
    

    Don't use OpenText with .CSV files directly, rename them first. Excel seems to work with CSV named files differently.

提交回复
热议问题