Druckansicht fürs Listview

  • Hey,

    hab hier was gebastelt um den ganzen Listviewinhalt auszudrucken.
    Das ganze funktioniert über eine .html Seite und da ich fast kein html kann ist die Formatierung wohl noch optimierungsfähig.
    Zum einfach mal schnell die Datensätze aus dem Listview auszudrucken ists aber ganz nützlich.
    Aber seht selbst:

    [Update: Start & End Index eingebaut - 18.09.2009]
    [Update: Schriftgröße eingebaut - 25.11.2011]


    Funktionen
    [autoit]


    Func _Listviewtoarray($hwnd)
    ; Nuts (http://www.autoit.de)
    ; hwnd = Handle to the Listview
    Local $col, $getcol, $getwidth, $lcount, $atext, $counter = 2
    $col = _GUICtrlListView_GetColumnCount($hwnd)
    $lcount = _GUICtrlListView_GetItemCount($hwnd)
    Local $alistview[$lcount + 2][$col]
    For $y = 0 To $col - 1
    $getcol = _GUICtrlListView_GetColumn($hwnd, $y)
    $getwidth = _GUICtrlListView_GetColumnWidth($hwnd, $y)
    $alistview[0][$y] = $getwidth
    $alistview[1][$y] = $getcol[5]
    Next
    For $i = 0 To $lcount - 1
    $atext = _GUICtrlListView_GetItemTextArray($hwnd, $i)
    For $x = 0 To $atext[0] - 1
    $alistview[$counter][$x] = $atext[$x + 1]
    Next
    $counter += 1
    Next
    Return $alistview
    $counter = 1
    EndFunc ;==>_Listviewtoarray

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ===================================================================
    ; Name ..........: _ArrayToHTMLTable2
    ; Description ...: Creates a Print-View HTML-table from a 2dim-array
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _ArrayToHTMLTable(ByRef $aArray [, $break = 37], )
    ; Parameter(s): .: $aArray - 2D Array
    ; $istart - Optional: Start-Index (Default = 0) First Item
    ; $iend - Optional: End-Index (Default = 0) Last Item
    ; $break - Optional: (Default = 37 -> DIN-A4 for IE) break after ...
    ; Return Value ..: Success - string
    ; Failure - empty string
    ; @ERROR - incorrect boarders ($istart & $iend)
    ; Author(s) .....: Nuts (http://www.autoit.de)
    ; Basis Funktion : Thorsten Willert _ArrayToHTMLTable
    ; Date ..........: 08.09.2009
    ; Version .......: 1.0
    ; ==============================================================================
    Func _ArrayToHTMLTable2(ByRef $aArray, $istart = 0, $iend = 0, $break = 37)
    Local $int, $theader, $width, $colwidth, $hcounter = 0, $del = $break + 1
    if not IsInt($istart) or not IsInt($iend) then
    SetError(2)
    Return
    endif
    Local $iD1 = UBound($aArray, 1)
    If @error Then
    SetError(1)
    Return ''
    EndIf
    Local $iD2 = UBound($aArray, 2)
    If @error Then
    SetError(1)
    Return ''
    EndIf
    For $i = 0 To $iD2 - 1
    $width += $aArray[0][$i]
    Next
    $colwidth &= '<colgroup>' & @CRLF
    For $i = 0 To $iD2 - 1
    $calc = 100 / $width * $aArray[0][$i]
    $colwidth &= @TAB & '<col width="' & $calc & '%">' & @CRLF
    Next
    $colwidth &= '</colgroup>' & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    Local $sTable = ""
    $sTable &= '<style type="text/css">' & @CRLF
    $sTable &= '</style>' & @CRLF
    $sTable &= '<table border="1" style="width:100%">' & @CRLF
    $sTable &= $colwidth
    $sTable &= '<tr>' & @CRLF
    For $i = 0 To $iD2 - 1
    $theader &= @TAB & '<th align="left">' & $aArray[1][$i] & '</th>' & @CRLF
    Next
    $sTable &= $theader
    $sTable &= '</tr>' & @CRLF
    If $iend > 0 and $iend < ($id1 -1) then
    $iend += 1
    elseif $iend = 0 then
    $iend = ($id1 -1)
    else
    SetError(2)
    return
    endif
    If $istart > 0 and $istart < $iend then
    $istart += 1
    elseif $istart = 0 Then
    $istart = 2
    else
    SetError(2)
    return
    endif
    For $i = $istart To $iend
    $hcounter += 1
    If $hcounter = $del Then
    $sTable &= '</table>' & @CRLF
    $sTable &= '<h5>Übertrag</h5>' & @CRLF
    $sTable &= '<h5 style="page-break-before:always">Übertrag</h5>' & @CRLF
    $sTable &= '<table border="1" style="width:100%">' & @CRLF
    $sTable &= $colwidth
    $sTable &= '<tr>' & @CRLF
    $sTable &= $theader
    $sTable &= '</tr>' & @CRLF
    $hcounter = 0
    $del = $break
    EndIf
    $sTable &= '<tr>' & @CRLF
    For $j = 0 To $iD2 - 1
    $sTable &= @TAB & '<td>' & $aArray[$i][$j] & '</td>' & @CRLF
    Next
    $sTable &= '</tr>' & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    Next

    [/autoit] [autoit][/autoit] [autoit]

    $sTable &= '</table>' & @CRLF
    $theader = ""
    $colwidth = ""
    Return $sTable
    EndFunc ;==>_ArrayToHTMLTable2

    [/autoit]
    Beispiel
    [autoit]


    #include<WindowsConstants.au3>
    #include<ListViewConstants.au3>
    #include<ButtonConstants.au3>
    #include<GUIConstantsEx.au3>
    #include<Guilistview.au3>
    #include<File.au3>
    #include<Array.au3>
    #include<IE.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $sHeader = "Artikelnummer|Art|Nummer|Baureihe" ; Die Überschriften für das Listview und für das "Neuer Eintrag"-Fenster
    Global $sDBFile = @ScriptDir & "\datenbank.html" ; Pfad und Name der Datenbank-Datei
    If Not FileExists($sDBFile) Then _FileCreate($sDBFile)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #Region Hauptfenster
    Global $hGui = GUICreate("Listview-Datenbank-Beispiel", 600, 480) ; Hauptfenster erstellen
    Global $hListView = GUICtrlCreateListView($sHeader, 0, 0, 600, 420, $LVS_SHOWSELALWAYS) ; Listview erstellen

    [/autoit] [autoit][/autoit] [autoit]

    Global $hLVHandle = GUICtrlGetHandle($hListView) ; das Handle vom Listview wird für die UDF-Listview-Funktionen benötigt
    _GUICtrlListView_SetColumn($hLVHandle, 0, "Artikelnummer", 100, 0)
    _GUICtrlListView_SetColumn($hLVHandle, 1, "Art", 60, 0)
    _GUICtrlListView_SetColumn($hLVHandle, 2, "Nummer", 90, 0)
    _GUICtrlListView_SetColumn($hLVHandle, 3, "Baureihe", 100, 0)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To 90
    GUICtrlCreateListViewItem($i & "|Test|2|Uiui", $hListView)
    Next
    Global $hNew = GUICtrlCreateButton("Print", 5, 430, 80, 35)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg(1) ; Message-Event holen (1) = erweiterter Modus
    Switch $nMsg[0] ; anhand der Control-ID das entsprechende Case aufrufen

    [/autoit] [autoit][/autoit] [autoit]

    Case $hNew
    Local $array = _Listviewtoarray($hLVHandle)
    ;_ArrayDisplay($array)
    _test($array)

    [/autoit] [autoit][/autoit] [autoit]

    Case $GUI_EVENT_CLOSE ; User hat auf das Schließen-Symbol geklickt (bzw. die ESC-Taste gedrückt)
    Switch $nMsg[1] ; erweiterte Abfrage für welches Fenster
    Case $hGui ; User will das Hauptfenster schließen
    Exit
    EndSwitch
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _test($array)
    Local $open = FileOpen($sDBFile, 2)
    Local $html = _ArrayToHTMLTable2($array, 0, 0, 41, 14)
    ;MsgBox(1, "", @error)
    FileWrite($open, $html)
    FileClose($open)
    ShellExecute($sDBFile)
    ;local $oIE = _IECreate ($sDBFile) ;Die Befehle zum senden an den Drucker habe ich zum Testen deaktiviert.
    ;_IEAction ($oIE, "print")
    ;_IELoadWait ($oIE)
    ;_IEQuit ($oIE)
    EndFunc ;==>_test

    [/autoit] [autoit][/autoit] [autoit]

    Func _Listviewtoarray($hwnd)
    ; Nuts (http://www.autoit.de)
    ; hwnd = Handle to the Listview
    Local $col, $getcol, $getwidth, $lcount, $atext, $counter = 2
    $col = _GUICtrlListView_GetColumnCount($hwnd)
    $lcount = _GUICtrlListView_GetItemCount($hwnd)
    Local $alistview[$lcount + 2][$col]
    For $y = 0 To $col - 1
    $getcol = _GUICtrlListView_GetColumn($hwnd, $y)
    $getwidth = _GUICtrlListView_GetColumnWidth($hwnd, $y)
    $alistview[0][$y] = $getwidth
    $alistview[1][$y] = $getcol[5]
    Next
    For $i = 0 To $lcount - 1
    $atext = _GUICtrlListView_GetItemTextArray($hwnd, $i)
    For $x = 0 To $atext[0] - 1
    $alistview[$counter][$x] = $atext[$x + 1]
    Next
    $counter += 1
    Next
    Return $alistview
    $counter = 1
    EndFunc ;==>_Listviewtoarray

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ===================================================================
    ; Name ..........: _ArrayToHTMLTable2
    ; Description ...: Creates a Print-View HTML-table from a 2dim-array
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _ArrayToHTMLTable(ByRef $aArray [istart = 0 [, $iend = 0 [, $break = 41 [, $fontsize=14]]]] )
    ; Parameter(s): .: $aArray - 2D Array
    ; $istart - Optional: Start-Index (Default = 0) First Item
    ; $iend - Optional: End-Index (Default = 0) Last Item
    ; $break - Optional: (Default = 41 -> DIN-A4 for IE) break after ...
    ; $fontsize - Optional (Default = 14)
    ; Return Value ..: Success - string
    ; Failure - empty string
    ; @ERROR - incorrect boarders ($istart & $iend)
    ; Author(s) .....: Nuts (http://www.autoit.de)
    ; Basis Funktion : Thorsten Willert _ArrayToHTMLTable
    ; Date ..........: 08.09.2009
    ; Version .......: 1.0
    ; ==============================================================================
    Func _ArrayToHTMLTable2(ByRef $aArray, $istart = 0, $iend = 0, $break = 41, $fontsize=14)
    Local $int, $theader, $width, $colwidth, $hcounter = 0, $del = $break + 1
    if not IsInt($istart) or not IsInt($iend) then
    SetError(1,0,0)
    Return
    endif
    Local $iD1 = UBound($aArray, 1)
    If @error Then
    SetError(2,0,0)
    Return ''
    EndIf
    Local $iD2 = UBound($aArray, 2)
    If @error Then
    SetError(2,0,0)
    Return ''
    EndIf
    For $i = 0 To $iD2 - 1
    $width += $aArray[0][$i]
    Next
    $colwidth &= '<colgroup>' & @CRLF
    For $i = 0 To $iD2 - 1
    $calc = 100 / $width * $aArray[0][$i]
    $colwidth &= @TAB & '<col width="' & $calc & '%">' & @CRLF
    Next
    $colwidth &= '</colgroup>' & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    Local $sTable = ""
    $sTable &= '<style type="text/css">' & @CRLF
    $sTable&= 'td {font-size:'&$fontsize&';}' &@crlf
    $sTable&= 'th {font-size:'&$fontsize&';}' &@crlf
    $sTable &= '</style>' & @CRLF
    $sTable &= '<table border="1" style="width:100%">' & @CRLF
    $sTable &= $colwidth
    $sTable &= '<tr>' & @CRLF
    For $i = 0 To $iD2 - 1
    $theader &= @TAB & '<th align="left">' & $aArray[1][$i] & '</th>' & @CRLF
    Next
    $sTable &= $theader
    $sTable &= '</tr>' & @CRLF
    If $iend > 0 and $iend < ($id1 -1) then
    $iend += 1
    elseif $iend = 0 then
    $iend = ($id1 -1)
    else
    SetError(3,0,0)
    return
    endif
    If $istart > 0 and $istart < $iend then
    $istart += 1
    elseif $istart = 0 Then
    $istart = 2
    else
    SetError(3,0,0)
    return
    endif
    For $i = $istart To $iend
    $hcounter += 1
    If $hcounter = $del Then
    $sTable &= '</table>' & @CRLF
    $sTable &= '<h5>Übertrag</'&$fontsize&'>' & @CRLF
    $sTable &= '<h5 style="page-break-before:always">Übertrag</h5>' & @CRLF
    $sTable &= '<table border="1" style="width:100%">' & @CRLF
    $sTable &= $colwidth
    $sTable &= '<tr>' & @CRLF
    $sTable &= $theader
    $sTable &= '</tr>' & @CRLF
    $hcounter = 0
    $del = $break
    EndIf
    $sTable &= '<tr>' & @CRLF
    For $j = 0 To $iD2 - 1
    $sTable &= @TAB & '<td>' & $aArray[$i][$j] & '</td>' & @CRLF
    Next
    $sTable &= '</tr>' & @CRLF

    [/autoit] [autoit][/autoit] [autoit]

    Next

    [/autoit] [autoit][/autoit] [autoit]

    $sTable &= '</table>' & @CRLF
    $theader = ""
    $colwidth = ""
    Return $sTable
    EndFunc ;==>_ArrayToHTMLTable2

    [/autoit]

    Noch nicht eingegangen wird auf die tatsächliche Listviewspaltenbreite. Im Update eingabut.

    Zum Testen müsst ihr die Druckansicht im Browser bemühen oder einen Testdruck machen.
    Die URL lässt sich leider bei html Dokumenten nicht automatisch entfernen (oder doch? hab nichts dazu gefunden), was irgendwie blöd aussieht :( s. Reg-tweaks

    Vorschläge für ein alternatives Format sind auch willkommen, mein Ziel ist eine Druckansicht ohne Fremdsoftware.

    Gruß nuts

    P.S. Bin schon gespannt aufs Andy's Lösung mit seiner Openoffice UDF :)


    [Update: Regtweaks] Irgendwie fällt mir dazu kein sinnvolles Konzept ein und in der Reg. rumpfuschen soll jeder selbst machen ;)
    Die Parameter sind hier nachzulesen: http://www.febooti.com/products/iezoo…ter-header.html
    Mit XP unter folgendem Key: Hkey_Current_User\Software\Microsoft\Internet Explorer\PageSetup & footer bzw. header

    4 Mal editiert, zuletzt von nuts (25. November 2011 um 14:36)

  • Ich finde das teil klasse und denke das ich das bestimmt mal gebrauchen könnte, was mich stört ist das es über dem internetexplorer gedruckt wird aber das macht ja nichts man kann sich gedanken machen wie es anders geht!

    Meine überlegung ist meine frau ( Freundin ) ist stationsleitung und schreib öfters mal dienstpläne und ist dann öfters länger auf arbeit was so ja kein problem darstellt nur habe ich mir zu aufgabe gemacht ein dienstplanprogramm zu coden mit M-office Excel habe ich das super hin bekommen nur es läuft in abhängigkeit und jeder kann sehen wie es aufgebaut ist.


    weiter so!

    LG Kleiner

    _

  • Freut mich das es dir gefällt.
    Es muss ja nicht über den IE gedruckt werden. Jede andere Möglichkeit eine .html Seite zu drucken ist verwendbar.

    Wie sieht denn die Seite im Firefox aus?

    edit \ Ist wohl nicht FF tauglich :(
    edit2 \ Naja beim FF ist die Grenze wohl bei 36 Items - irgendwie alles schon ein Murks :D

    Einmal editiert, zuletzt von nuts (11. September 2009 um 11:00)

  • Also es gibt doch ein Möglichkeit die Url usw. zu entfernen!
    Dazu müssen bestimmte Reg.-Schlüssel geändert werden:

    Hkey_Current_User\Software\Microsoft\Internet Explorer\PageSetup
    Dort den Wert für "footer" & "header" entfernen.

    Kann man sich per Autoit ins Skript einbauen und nach dem Drucken zurücksetzen (oder so ähnlich).

  • nuts,
    sehr schön!!!
    Um "mal eben" eine kleine Liste auszudrucken völligst ausreichend, und da soll ich mich nun stundenlang hinsetzen und eine Druckfunktion für OO/Excel ausklabustern? Naja, schaumamal :rock:
    ciao
    andy

    ciao
    Andy


    "Schlechtes Benehmen halten die Leute doch nur deswegen für eine Art Vorrecht, weil keiner ihnen aufs Maul haut." Klaus Kinski
    "Hint: Write comments after each line. So you can (better) see what your program does and what it not does. And we can see what you're thinking what your program does and we can point to the missunderstandings." A-Jay

    Wie man Fragen richtig stellt... Tutorial: Wie man Script-Fehler findet und beseitigt...X-Y-Problem

    Einmal editiert, zuletzt von Andy (11. September 2009 um 17:42)

  • Hey,

    naja stundenlang musste dich dafür nicht hinsetzen ;)
    Sollte es irgendwann doch mal soweit sein, hoffe ich doch das Skript wird den Weg ins Forum finden?

    Morgen bastel ich noch die Reg. -Tweaks und einen Bezug zur tatsächlichen Spaltenbreite ein.
    Für den Firefox müsste man dann wieder andere Reg.-Änderungen vornehmen (oder gibts dafür eine elegantere Lösung?), ich denke das kann dann jeder selbst einbauen.

  • Update (s. Post1): Bezug zur Listviewspaltenbreite eingebaut.

    Das mit den Reg-tweaks ist so ne Sache.
    Beim Zurücksetzen muss der Druckvorgang schon beendet sein, sonst wirkts irgendwie nicht.
    Weiss jemand wie man den Druckerstatus abfragen kann?

    Einmal editiert, zuletzt von nuts (18. September 2009 um 14:02)