spongebob24
Goto Top

Zahlen in Excel per Powershell umwandeln in Barcodes

Hallo zusammen,

Ich hätte da ein anliegen und wollte mal die Powershellprofis hier fragen, ob es grundsätzlich möglich wäre.
Ich habe tägliche Excelfiles, in denen in Spalte B mehrer Codes ausgegeben werden. ( 376103140001462321)

Nun die eigentlich Frage, ist es möglich aus diesen per Powershell einen Barcode in zb. Spalte E zu generieren lassen?

SG

Content-Key: 592032

Url: https://administrator.de/contentid/592032

Printed on: April 19, 2024 at 06:04 o'clock

Member: BirdyB
BirdyB Jul 30, 2020 at 07:10:17 (UTC)
Goto Top
Moin,

würde es nicht genügen, ein Barcode-Font zu installieren und die Excel-Zelle entsprechend zu formatieren?

VG
Member: spongebob24
spongebob24 Jul 30, 2020 at 07:16:20 (UTC)
Goto Top
Hi,

jein Bequeme User face-wink
Member: godlie
godlie Jul 30, 2020 at 07:27:08 (UTC)
Goto Top
Hallo,

naja das scheint nicht gearde der "Aufwand" zu sein, siehe hier
Member: spongebob24
spongebob24 Jul 30, 2020 at 07:30:25 (UTC)
Goto Top
Cool Danke werde ich mir anschauen.

Scheins ist meine google Skill noch im Bett face-wink

Danke und Schönen Tag
Member: colinardo
colinardo Jul 30, 2020, updated at Jul 31, 2020 at 08:15:38 (UTC)
Goto Top
Servus @spongebob,
ob es grundsätzlich möglich wäre.
ja kein Problem. Da du jetzt keinen konkreten Barcodetyp genannt hast hier mal ein Beispiel mit einem Code-128.
# Quelldatei
$quelle = 'D:\test\data.xlsx'  

# Funktion zum Laden von Assemblies von Nuget.org
function Load-NugetAssembly {
    [CmdletBinding()]
    param(
        [string]$url,
        [string]$name,
        [string]$zipinternalpath,
        [switch]$downloadonly
    )
    # Accept all TLS protocols
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::GetNames([System.Net.SecurityProtocolType]) 
    if($psscriptroot -ne ''){  
        $localpath = join-path $psscriptroot $name
    }else{
        $localpath = join-path $env:TEMP $name
    }
    $tmp = "$env:TEMP\$([IO.Path]::GetRandomFileName())"  
    $zip = $null
    try{
        if(!(Test-Path $localpath)){
            Add-Type -A System.IO.Compression.FileSystem
            write-host "Downloading and extracting required library '$name' ... " -F Green -NoNewline  
            (New-Object System.Net.WebClient).DownloadFile($url, $tmp)
            $zip = [System.IO.Compression.ZipFile]::OpenRead($tmp)
            $zip.Entries | ?{$_.Fullname -eq $zipinternalpath} | %{
                [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_,$localpath)
            }
            write-host "OK" -F Green  
        }
        if(!$downloadonly.IsPresent){
            Add-Type -Path $localpath -EA Stop
        }
        
    }catch{
        throw "Error: $($_.Exception.Message)"  
    }finally{
        if ($zip){$zip.Dispose()}
        if(Test-Path $tmp){del $tmp -Force}
    }  
}

# Lade ZXING Assembly
if (!('ZXing' -as [Type])){  
        Load-NugetAssembly -url 'https://www.nuget.org/api/v2/package/ZXing.Net' -name 'zxing.dll' -zipinternalpath 'lib/net40/zxing.dll' -EA Stop  
}

# Funktion zum Erstellen von Barcodes
function New-Barcode([ZXing.BarcodeFormat]$format = ([ZXing.BarcodeFormat]::CODE_128),[string]$data,[int]$width,[int]$height,[int]$margin = 0,[switch]$purebarcode) {
    $writer = New-Object ZXing.BarcodeWriter -Property @{
        Format = $format
        Options = New-Object ZXing.Common.EncodingOptions -Property @{
            Height = $height
            Width = $width
            PureBarcode = $purebarcode.IsPresent
            Margin = $margin
        }
    }
    $writer.Write($data)
}

# Excel Objekt
$objExcel = New-Object -Com Excel.Application -Property @{Visible = $false; DisplayAlerts = $false}
# Mappe öffnen
$wb = $objExcel.Workbooks.Open($quelle)
# Arbeitsblatt 1 nutzen
$ws = $wb.Sheets.Item(1)

# Alle belegten Zellen in Spalte B ab B2 durchlaufen
$ws.Range("B2:B" + $ws.Cells($ws.Rows.Count,"B").End(-4162).Row) | ?{$_.Text -ne ''} | %{  
    $img = $null
    try{
        # temporärer Zielpfad für Barcodeimage
        $bcpath = "$env:Temp\$($_.Text).png"  
        # Barcode soll 3 Spalten weiter rechts eingefügt werden
        $rngInsert = $_.Offset(0,3)
        write-host "Creating barcode for value $($_.Text) in cell $($rngInsert.Address())."  
        # generiere den Barcode als In-Memory Bitmap
        $img = New-Barcode -format CODE_128 -data $_.Text -height 50 -width 150
        # speichere den Barcode als *.png
        $img.Save($bcpath,'Png')  
        # füge den Barcode als Bild an der gewünschten Position in Originalgröße ein
        $pic = $ws.Shapes.AddPicture($bcpath,$false,$true,$rngInsert.Left,$rngInsert.Top,-1,-1)
        # setze die Zeilenhöhe auf die Höhe des Barcodes
        $rngInsert.RowHeight = $pic.Height
    }catch{
        write-Error $_.Exception.Message
    }finally{
        if($img){$img.Dispose()}
    }
}

# speichere das Workbook
write-host "Saving workbook." -f Green  
$wb.Save()
$wb.Close($false)
# Excel schließen
$objExcel.DisplayAlerts = $true
$objExcel.Quit()
# Ressourcen freigeben
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($objExcel)
Grüße Uwe