如何在PowerShell中打开新版本的Excel(我使用2003和2010),并将TXT数据导入到Excel文件中?

我使用下面的代码将txt文件导入到excel中。 问题是我使用Office(2003和2010)的两个版本。

命令$ Excel = New-Object -ComObject excel.application打开Excel 2003我需要使用Excel 2010而不是2003并导入新数据。 我的Excel 2010存储在C:\ Program Files \ Microsoft Office \ Office14 \ excel.exe中

只有这样,我才能在PowerShell中打开Excel 2010

Invoke-Item "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" 

但是,我的代码的其余部分是忽略Excel 2010.它将打开并导入Excel 2003的数据。 也从改变

  $objExcel = New-Object -COMObject Excel.Application.11 

  $objExcel = New-Object -COMObject Excel.Application.14 

不起作用。

感谢您的任何帮助。

史蒂夫

 $CSVfile = "E:\test.txt" #Create a new Excel object and add a new workbook. #Delete the extra worksheets and rename the first worksheet. $Excel = New-Object -ComObject excel.application $Excel.visible = $true $workbooks = $excel.Workbooks.Add() $worksheets = $workbooks.worksheets $worksheets.Item(3).delete() $worksheets.Item(2).delete() $worksheet = $worksheets.Item(1) $worksheet.Name = "Name of Worksheet" #Define the connection string and where the data is supposed to go $TxtConnector = ("TEXT;" + $CSVfile) $CellRef = $worksheet.Range("A1") #Build, use and remove the text file connector $Connector = $worksheet.QueryTables.add($TxtConnector,$CellRef) $worksheet.QueryTables.item($Connector.name).TextFileCommaDelimiter = $True $worksheet.QueryTables.item($Connector.name).TextFileParseType = 1 $worksheet.QueryTables.item($Connector.name).Refresh() $worksheet.QueryTables.item($Connector.name).delete() $worksheet.UsedRange.EntireColumn.AutoFit()