#Add-Type -Path "Syncfusion.XlsIO.Portable.dll"
#Add-Type -Path "Syncfusion.Compression.Portable.dll"

function TestExcel {
    $inputFilePath = "/Users/edmond/Develop/PowerShell/BIT-BV/Data/Z999/In/bitBVTest.xlsx"
    $asciiOutputFilePath = "/Users/edmond/Develop/PowerShell/BIT-BV/Data/Z999/In/bitBVTest-ASCII.csv"
    $utf8OutputFilePath = "/Users/edmond/Develop/PowerShell/BIT-BV/Data/Z999/In/bitBVTest-UTF8.csv"
    $excelInputFile = New-Object -TypeName System.IO.FileStream -ArgumentList @($inputFilePath, [System.IO.FileMode]::Open)
    Remove-Item -Path $asciiOutputFilePath -ErrorAction Ignore
    Remove-Item -Path $utf8OutputFilePath -ErrorAction Ignore
    $excelEngine = New-Object Syncfusion.XlsIO.ExcelEngine 
    $excelEngine.Excel.DefaultVersion = [Syncfusion.XlsIO.ExcelVersion]::Excel2013
    $workBook = $excelEngine.Excel.Workbooks.Open($excelInputFile, [Syncfusion.XlsIO.ExcelOpenType]::Automatic)
    $workSheet = $workBook.Worksheets["Tabelle1"]
    $rowCount = $workSheet.UsedRange.LastRow
    $colCount = $workSheet.UsedRange.LastColumn

    for ($i = 2; $i -le $rowCount; $i++) { 
        for ($j = 1; $j -le $colCount; $j++) { 
            $value = $workSheet.Range[$i, $j].DisplayText
            $newValue = $value -replace "`n", " --- " -replace "`r", " --- " -replace ";", "--" -replace "`"", "'"
            # $newValue = convertUTF8ToASCII $newValue
            $workSheet.Range[$i, $j].Text = $newValue
        } 
    } 
    $utf8Stream = [System.IO.File]::Create($utf8OutputFilePath);
    $asciiStream = [System.IO.File]::Create($asciiOutputFilePath);
    $workSheet.SaveAs($utf8Stream, ";", [System.Text.Encoding]::UTF8)
    $workSheet.SaveAs($asciiStream, ";", [System.Text.Encoding]::ASCII)

    $utf8Stream.Close()
    $asciiStream.Close()
    $workBook.Close($true)
}

