I see this type of question asked and answered many times but not in Powershell and couldn't find the answer, so...
I'm converting an XLSX file to CSV using the following code :
$sheet = "SHEET1"
$xls = "C:\temp\XLSX.xlsx"
$csv = "C:\temp\CSV.csv"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $False
$objExcel.DisplayAlerts = $False
$WorkBook = $objExcel.Workbooks.Open($xls)
$WorkSheet = $WorkBook.sheets.item("$Sheet")
$xlCSV = 6
$WorkSheet.SaveAs($csv,$xlCSV)
$objExcel.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
That conversion works fine except for dates. The XLSX file contains dates with format dd/MM/yyyy but after the above code saves my sheet to CSV those dates end up having the format M/d/yyyy. This of course causes problems later when I import my CSV and execute the rest of the script. Many dates also become "not recognized as a valid DateTime".
Can I add something to the code so those dates will remain in the same format ?