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
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
; #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
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
Next
[/autoit] [autoit][/autoit] [autoit]$sTable &= '</table>' & @CRLF
$theader = ""
$colwidth = ""
Return $sTable
EndFunc ;==>_ArrayToHTMLTable2
Beispiel
#include<WindowsConstants.au3>
#include<ListViewConstants.au3>
#include<ButtonConstants.au3>
#include<GUIConstantsEx.au3>
#include<Guilistview.au3>
#include<File.au3>
#include<Array.au3>
#include<IE.au3>
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)
#Region Hauptfenster
Global $hGui = GUICreate("Listview-Datenbank-Beispiel", 600, 480) ; Hauptfenster erstellen
Global $hListView = GUICtrlCreateListView($sHeader, 0, 0, 600, 420, $LVS_SHOWSELALWAYS) ; Listview erstellen
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)
For $i = 1 To 90
GUICtrlCreateListViewItem($i & "|Test|2|Uiui", $hListView)
Next
Global $hNew = GUICtrlCreateButton("Print", 5, 430, 80, 35)
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
Case $hNew
Local $array = _Listviewtoarray($hLVHandle)
;_ArrayDisplay($array)
_test($array)
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
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
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
; #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
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
Next
[/autoit] [autoit][/autoit] [autoit]$sTable &= '</table>' & @CRLF
$theader = ""
$colwidth = ""
Return $sTable
EndFunc ;==>_ArrayToHTMLTable2
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