简体   繁体   中英

Copy data from excel to outlook using powershell

I tried the following code but it pastes some garbage text while copying data from excel to outlook. Using powershell. Code used:

$body=""
get-content "C:\Users\smi00019\Desktop\AO\Book1.xlsx" | foreach{$body+="$_`n"}

Excel data:

Name    Place   Animal
ABC     Mumbai  Dog
XYZ     Pune    Cat

I am trying to copy above data range A1:c3

Get-Content is for use with text-based files. Excel files are not text based and contain other elements (formatting, formulas, macros, graphs etc)

I would recommend using the PSExcel Module as it contains the Import-XLSX function which makes working with Excel files very easy.

Import-XLSX works like Import-CSV , and generates an 'array' object from the file.

Excel:

在此处输入图片说明

$Imported = Import-XLSX -Path C:\Temp\Demo.xlsx -Header samaccountname, EID, Date

PS Object:

在此处输入图片说明


You can then use Select-Object to get the Property (column names) you want and that you only the First two entries (rows).

$Imported | Select-Object -Property Column1,Column1,Column1 -First 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM