1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| # Déclaration des variables
$NomFichierXL=$("Le chemin et le nom du fichier XLSX")
$SheetName = "Nom de la feuille 1 "
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
#$workbook = $NomFichierXL
# Load the WorkSheet 'BuildSpecs'
$WorkSheet = $WorkBook.sheets.item($SheetName)
function Convert-ExcelToCsv([string]$NomFichierXL,[switch]$NotConvertSeparator)
{
if (!(test-path $NomFichierXL))
{ Write-Warning "Le fichier $NomFichierXL n'existe pas."; return}
#On crée un fichier temporaire afin d'obtenir un nom de fichier unique puis
#on le supprime sinon la méthode SaveAs d'Excel demandera une confirmation de suppression
#Ce fichier contient les données au format texte séparées par un point-virgule
$NomFichierTmp = [System.IO.Path]::GetTempFileName()
if (test-path $NomFichierTmp) {remove-item $NomFichierTmp}
#Construit le nom de fichier CSV
#Ce fichier contiendra les données au format texte séparées par une virgule sauf si $NotConvertSeparator est présent
#dans ce cas il contiendra les données au format texte séparées par un point-virgule
$NomFichierCsv=[System.IO.Path]::ChangeExtension($NomFichierXL,".csv")
#Supprime l'ancien fichier csv
if (test-path $NomFichierCsv) { remove-item $NomFichierCsv }
#Exécute Excel, en mode invisible par défaut. Ses fenêtres d'erreurs ou de confirmation peuvent toutefois s'afficher.
$Excel = new-object -com Excel.Application
#Ouvre le fichier source
$Classeur=$Excel.Workbooks.Open($NomFichierXL)
#En cas de pb d'accés la variable $Classeur n'est pas initialisée
if ($Classeur -eq $Null) { Write-Warning "Traitement interrompu."; return}
#<-- Paramétres de la méthode SaveAS
#Récupére le format de sauvegarde de l'énumération xlFileFormat Ã* partir de l'assembly interop Excel
$FileFormat=[Microsoft.Office.Interop.Excel.xlFileFormat]::xlCsv
#Missing indique pour un objet COM un paramètre absent. [System.Type]::Missing] n'est pas égal Ã*
$Null
$Password=[System.Type]::Missing
$WriteResPassword=[System.Type]::Missing
$ReadOnlyRecommended=$False
$CreateBackup=$False
$AddToMru=$False
$TextCodepage=[System.Type]::Missing
$TextVisualLayout=[System.Type]::Missing
$LangueLocal=$False
#-->
#On choisi d'enregistrer la feuille sélectionnée
# Version simplifiée : $Classeur.SaveAs($NomFichierTmp,$FileFormat)
$Classeur.SaveAs($NomFichierTmp,
$FileFormat,
$Password,
$WriteResPassword,
$ReadOnlyRecommended,
$CreateBackup,
$AddToMru,
$TextCodepage,
$TextVisualLayout,
$LangueLocal)
#Le changement de format de fichier provoque une alerte
$Excel.DisplayAlerts=$False
#Quitte et force la libération de l'instance d'Excel
$Excel.Quit()
$Excel=$Null
[GC]::Collect()
if ($NotConvertSeparator.isPresent)
#Le fichier tmp est crée dans le répertoire %TEMP%/%TMP%
{ Copy-Item $NomFichierTmp $NomFichierCsv }
else
#Modifie le séparateur pour un usage avec Import-Csv
{ get-content $NomFichierTmp|% {$_ -replace ';',','}| out-file $NomFichierCsv }
#Supprime le fichier transitoire créé par Excel
if (test-path $NomFichierTmp) { remove-item $NomFichierTmp}
}
$Error.clear()
Convert-ExcelToCsv $NomFichierXL
if ($Error.count -ne 0) {return}
## Extrait du nombre de tache restante dans l'onglet Data
$SheetName1 = "Nom de la feuille 2"
# Open the Excel file and save it in $WorkBook
$WorkBook1 = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'BuildSpecs'
$WorkSheet1 = $WorkBook1.sheets.item($SheetName1)
$nbTache = $WorkSheet1.range("b2").Text
# Déclaration des variables pour l'envoi du mail
$expediteur = 'Adresse expéditeur'
$destinataire = 'Adresse destinataire'
$smtp = 'smtp'
$objet = 'Tache a faire '
$LeTableau = import-csv "Le chemin et le nom du fichier csv" | ConvertTo-Html
$UTF8=[System.Text.Encoding]::UTF8
## L'envoi du mail
send-mailmessage -to $destinataire -from $expediteur -Subject $objet -Encoding $UTF8 -SmtpServer $smtp -bodyashtml "$LeTableau <th>Il vous reste : $nbTache Tâches</th>"
Stop-Process -processname excel
rm "Le chemin et le nom du fichier cs" |
Partager