Oct 172016
 

Amusant. Si vous faites « enregistrer sous » et choisissez le format « CSV » et que vous avez des caractères bizarres ( Comme é,è,&,..) Excel les détruira lors de l’enregistrement (pas sympa)

Si vous faites « fichier > exporter » et choisissez le format CSV, ca va mieux! mais c’est pas encore ça.. (les champs ne sont pas entre guillemets et on ne peux pas choisir le caractère de séparation…

Voici donc un script PowerShell qui fait le boulot…

Et pour que ce soit GENIAL, faites un raccourci windows avec cette commande:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -ExecutionPolicy Bypass   -file  « H:\Scripts\XLS to CSV.ps1 »
(pensez à changer le chemin du script)
Ainsi, il vous suffira de faire un drag’n drop de votre fichier excel sur ce raccourci et HOP! converti !
(on pourait aussi envisager de le mettre dans « send to » mais, ca, je vous laisse faire)

param([string]$xlFile=’nothing’)
$debugMode=$false
$ErrorActionPreference=’Stop’
if ($debugMode) { $xlFile= »H:\Book1.xlsx » }
$csvFile=$xlfile.Replace(‘.xlsx’,’.csv’).replace(‘.xls’,’.csv’)
## Checking…
if ( $xlFile -eq ‘nothing’ ) { Write-Host « No Excel file given. aborting… »                                                     -foregroundcolor red ;$tmp=Read-Host « Press ENTER key to exit »;exit }
if ( test-path($xlFile -eq $false)  ) { Write-Host « Specified Excel file does not exist. »                                        -foregroundcolor red ;$tmp=Read-Host « Press ENTER key to exit »;exit }
if ( test-path($csvFile)   ) { Write-Host « Old converted CSV file exist. Please, delete it before trying to convert it again. »   -foregroundcolor red ;$tmp=Read-Host « Press ENTER key to exit »;exit }
# http://stackoverflow.com/questions/687891/exception-automating-excel-2007-with-powershell-when-calling-workbooks-add
& {
[threading.thread]::CurrentThread.CurrentCulture = ‘en-US’
$xl = New-Object -COM « Excel.Application »
$xl.Visible = $debugMode
Write-Host « Opening source file » -foregroundcolor green
$wb = $xl.Workbooks.Open($xlFile)
$ws = $wb.Sheets.Item(1)
  $i=1
while( $ws.Cells.Item(1, $i).value2 -ne $null ) { if ( $debugMode) {Write-host « Debug 1 : counting column $i »}; $i++ }
$maxColonne=$i-1
  $i=1
while( $ws.Cells.Item($i, 1).value2 -ne $null ) { if ( $debugMode) {Write-host « Debug 1 : counting row $i »}; $i++ }
$maxLigne=$i
  Write-Host « There will be $maxColonne columns and $MaxLigne lines to convert. » -foregroundcolor green
$object=@()
$ligne=2   # ligne 1 = titre
$colonne=1
  while( $ws.Cells.Item($ligne,1).value2 -ne $null ) {
$line=New-Object PSObject
$colonne=1
while( $colonne -le $maxColonne) {
$cell=$ws.Cells.Item($ligne,$colonne).value2
if ($cell -eq $null) { $cell = «  » }
Add-Member -InputObject $line -MemberType NoteProperty -Name $ws.Cells.Item(1, $colonne).value2 -Value $cell
if ( $debugMode) {Write-host « debug2  » + $ws.Cells.Item($ligne,$colonne).value2 }
$colonne++
}
$ligne++
$object += $line
write-host -NoNewline « `r                                              `r Progres : $ligne/$maxLigne »
}
  $wb.Close() | out-null
$xl.Quit() | out-null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) | out-null
  Write-Host « `r`n$ligne rows where converted. » -foregroundcolor green
Write-Host « Saving datas to $csvFile. Please, wait… » -foregroundcolor cyan
$object | Export-Csv $csvFile -NoTypeInformation -Delimiter ‘;’ -Encoding UTF8
Write-Host « Save completed. » -foregroundcolor green
sleep 3
}
Tadaaaaa…

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)