Finding Powershell names for Excel features

后端 未结 2 1019
小蘑菇
小蘑菇 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.

    0 讨论(0)
  • 2021-01-28 02:29

    Menu item names are there for the convenience of the user; labels you see there may or may not correspond directly to the COM methods you need to call. From any kind of a program (including a PowerShell script), you need to be interacting with the object model that the application exposes. Microsoft has Excel's documented at http://msdn.microsoft.com/en-us/library/wss56bz7.aspx

    That said, depending on how many files you're working with and what exactly you need to do with them, going this route may be very slow and tedious. In many cases, it may be faster to treat Excel as an ODBC data source and insert your data into spreadsheets as though they were databases.

    0 讨论(0)
提交回复
热议问题