Hey everyone,
i have following problem: i’m working on a script that fully automatically copy and pastes content from a CSV to a xlsx. Basically everytime something new gets written in the CSV data the script copies and pastes it to the xlsxv data. All of that is actually working but the whole content in the data where it is pasted is still encrypted. I usually encode with utf8 which is the standart but i also tried utf32, ASCII etc…but nothing worked. I already researched everything and asked multiple AI’s but somehow nothing really helped me. Thanks for the help in advance ![]()
This is the code i’m using:
$csvpath = “C:\Users\J549180\Desktop\Projekte\CDR Daten extrahieren\Testdaten.xlsx”
$excelpath = “C:\Users\J549180\Desktop\Projekte\CDR Daten extrahieren\Testendpunkt.xlsx”
$logPath = “C:\Users\J549180\Desktop\Projekte\CDR Daten extrahieren\logPath.txt”
$lastWriteTime = $null
function Copy-CSVToExcel {
param (
[string]$csvFile,
[string]$excelFile
)
if (Test-Path $csvFile) {
try {
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
$csvData = Import-Csv $csvFile -Encoding utf8 -Delimiter ","
# Schreibe Header
$col = 1
foreach ($key in $csvData[0].PSObject.Properties.Name) {
$worksheet.Cells.Item(1, $col).Value2 = $key
$col++
}
# Schreibe Daten
$row = 2
foreach ($entry in $csvData) {
$col = 1
foreach ($key in $entry.PSObject.Properties.Name) {
$worksheet.Cells.Item($row, $col).Value2 = $entry.$key
$col++
}
$row++
}
$workbook.SaveAs($excelFile, 51)
$workbook.Close($false)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
Add-Content $logPath "[$(Get-Date)] CSV wurde erfolgreich in Excel kopiert."
} catch {
Add-Content $logPath "[$(Get-Date)] Fehler beim Kopieren der CSV nach Excel: $_"
}
} else {
Add-Content $logPath "[$(Get-Date)] CSV nicht gefunden."
}
}
while ($true) {
$currentWriteTime = (Get-Item $csvPath).LastWriteTime
if ($lastWriteTime -ne $currentWriteTime) {
Copy-CSVToExcel -csvFile $csvPath -excelFile $excelPath
$lastWriteTime = $currentWriteTime
}
Start-Sleep -Seconds 60