Inhalt aus Treeview in *.docx exportieren

  • .NET (FX) 4.5–4.8
  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Paulus.

    Inhalt aus Treeview in *.docx exportieren

    Hallo liebe Forum Nutzer,

    ich habe mit ein Tool erstellt womit ich mir ein beliebiges Verzeichnis mit unterverzeichnissen und Inhalt in einen Treeview anzeigen lassen kann.
    Der Treewiev export geht einen Umweg, da ich es nicht anders hinbekomme. Ich exportiere erst den Treewiev in HTML und dann lese ich die HTML aus und
    speicher es in *docx. Soweit funktioniert es, aber ich bekomme es nicht hin, das die Umlaute richtig dargestellt werden, und die schrift in *docx Arial ist sowie die Ordner alle FETT geschrieben sind.
    Hatte einiges Probiert in UFT8 zu schreiben, aber egal was ich mache nix funzt.

    Hat jemand von euch villeicht ein Lösungsvorschlag?

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Explicit On
    2. Imports System.IO
    3. Imports System.Net
    4. Imports System.Text
    5. Imports System.Web
    6. Public Class Form1
    7. Dim Speichername As String
    8. Dim Pfad As String
    9. Dim Test As String
    10. Dim x As Integer
    11. Dim DerPfad As String
    12. Dim subOrdner As Integer
    13. Dim encoding As Encoding = Encoding.UTF8
    14. Dim writer As New StreamWriter("D:\Export.html", False, encoding)
    15. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    16. Try
    17. Cursor = Cursors.Hand
    18. AllesExpandierenToolStripMenuItem.Enabled = False
    19. AlleOrdnerSchließenToolStripMenuItem.Enabled = False
    20. ExportZuWordToolStripMenuItem.Enabled = False
    21. x = 0
    22. subOrdner = 0
    23. Catch ex As Exception
    24. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    25. End Try
    26. End Sub
    27. Private Sub FillTreeNode(ByVal dNode As TreeNode)
    28. Try
    29. Dim d As New DirectoryInfo(dNode.FullPath)
    30. For Each di As DirectoryInfo In d.GetDirectories
    31. Dim nNode As New TreeNode(di.Name)
    32. dNode.Nodes.Add(nNode)
    33. FillTreeNode(nNode)
    34. Next
    35. For Each fi As FileInfo In d.GetFiles
    36. Dim fNode As New TreeNode(fi.Name)
    37. dNode.Nodes.Add(fNode)
    38. Test = Test & dNode.ToString
    39. Next
    40. Catch ex As Exception
    41. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    42. End Try
    43. End Sub
    44. Private Sub ÖffnenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ÖffnenToolStripMenuItem.Click
    45. Try
    46. If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
    47. Cursor = Cursors.WaitCursor
    48. Dim nNode As New TreeNode(System.IO.Path.GetFullPath(FolderBrowserDialog1.SelectedPath))
    49. Pfad = FolderBrowserDialog1.SelectedPath.ToString
    50. TreeView1.Nodes.Add(nNode)
    51. Me.FillTreeNode(nNode)
    52. AllesExpandierenToolStripMenuItem.Enabled = True
    53. ExportZuWordToolStripMenuItem.Enabled = True
    54. Cursor = Cursors.Hand
    55. Else
    56. Exit Sub
    57. End If
    58. Catch ex As Exception
    59. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    60. End Try
    61. End Sub
    62. Private Sub BlattLeerenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BlattLeerenToolStripMenuItem.Click
    63. TreeView1.Nodes.Clear()
    64. End Sub
    65. Private Sub AllesExpandierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AllesExpandierenToolStripMenuItem.Click
    66. Try
    67. Cursor = Cursors.WaitCursor
    68. TreeView1.ExpandAll()
    69. AllesExpandierenToolStripMenuItem.Enabled = False
    70. AlleOrdnerSchließenToolStripMenuItem.Enabled = True
    71. Cursor = Cursors.Hand
    72. Catch ex As Exception
    73. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    74. End Try
    75. End Sub
    76. Private Sub AlleOrdnerSchließenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AlleOrdnerSchließenToolStripMenuItem.Click
    77. Try
    78. Cursor = Cursors.WaitCursor
    79. TreeView1.CollapseAll()
    80. AllesExpandierenToolStripMenuItem.Enabled = True
    81. AlleOrdnerSchließenToolStripMenuItem.Enabled = False
    82. Cursor = Cursors.Hand
    83. Catch ex As Exception
    84. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    85. End Try
    86. End Sub
    87. Private Sub ExportZuWordToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportZuWordToolStripMenuItem.Click
    88. Try
    89. Cursor = Cursors.WaitCursor
    90. 'Request a file name to save to
    91. Dim sfd As New SaveFileDialog()
    92. Dim saveFileName As String = String.Empty
    93. With sfd
    94. .FileName = "Export"
    95. .Title = "Export to *.docx File"
    96. .Filter = "Word document (*.docx)|*.html"
    97. .OverwritePrompt = True
    98. If .ShowDialog = Windows.Forms.DialogResult.OK Then
    99. saveFileName = .FileName
    100. Else
    101. Cursor = Cursors.Hand
    102. Exit Sub
    103. End If
    104. Speichername = saveFileName.ToString
    105. End With
    106. sfd.Dispose()
    107. If saveFileName.Length > 0 Then
    108. 'Used to store the nodes contents
    109. Dim treeContents As New System.Text.StringBuilder
    110. 'Start the HTML file
    111. treeContents.AppendLine("<html>")
    112. treeContents.AppendLine("<html>")
    113. treeContents.AppendLine("<head><title>Tree View Export</title></head>")
    114. treeContents.AppendLine("<body>")
    115. 'Enumerate through each root node.
    116. Dim treeEnumerator As IEnumerator = TreeView1.Nodes.GetEnumerator
    117. 'Export the tree view to the treeContents string builder
    118. Do Until treeEnumerator.MoveNext = False
    119. 'Pass the current root node to the OutputTreeNodesToString routine.
    120. OutputTreeNodesToHTMLString(DirectCast(treeEnumerator.Current, TreeNode), 0, treeContents)
    121. Loop
    122. 'Finish the HTML file
    123. treeContents.AppendLine("</body>")
    124. treeContents.AppendLine("</html>")
    125. 'Write the contents of the treeContents string builder to a new file
    126. Dim sw As New System.IO.StreamWriter(saveFileName)
    127. sw.Write(treeContents.ToString)
    128. sw.Dispose()
    129. End If
    130. Me.WebBrowser1.Navigate(Speichername)
    131. Application.DoEvents()
    132. Dim wc As New WebClient()
    133. Dim appWord As Object
    134. Dim doc As Object
    135. appWord = CreateObject("Word.Application")
    136. appWord.Visible = True
    137. doc = appWord.Documents.Add
    138. With doc
    139. .Range.Text = WebBrowser1.Document.Body.InnerText
    140. End With
    141. 'speichern
    142. Dim Speichername2 As String
    143. Speichername2 = (Speichername.Substring(0, Speichername.Length - 5))
    144. Speichername2 = Speichername2 & ".docx"
    145. doc.SaveAs(Speichername2)
    146. doc = Nothing
    147. 'html löschen, Word beenden
    148. My.Computer.FileSystem.DeleteFile(Speichername)
    149. appWord.Quit()
    150. Cursor = Cursors.Hand
    151. MessageBox.Show("Export nach " & Speichername2 & " abgeschlossen.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
    152. Catch ex As Exception
    153. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    154. End Try
    155. End Sub
    156. Private Sub OutputTreeNodesToHTMLString(ByVal currentNode As TreeNode, ByVal nodeDepth As Int32, ByRef treeContentsStringBuilder As System.Text.StringBuilder)
    157. Try
    158. If currentNode IsNot Nothing Then
    159. 'Retrieve an enumerator for the current node.
    160. Dim nodeEnumerator As IEnumerator = currentNode.Nodes.GetEnumerator
    161. 'Write the current node to the string builder using no breaking spaces (&nbsp;) based on the nodeDepth value.
    162. For i As Int32 = 0 To nodeDepth
    163. treeContentsStringBuilder.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
    164. Next
    165. treeContentsStringBuilder.Append(New String(ControlChars.VerticalTab, nodeDepth) & currentNode.Text & "<br />")
    166. 'If the currentNode has child nodes, increment the nodeDepth before recalling this routine.
    167. If currentNode.Nodes.Count > 0 Then nodeDepth += 1
    168. Do Until nodeEnumerator.MoveNext = False
    169. OutputTreeNodesToHTMLString(DirectCast(nodeEnumerator.Current, TreeNode), nodeDepth, treeContentsStringBuilder)
    170. Loop
    171. End If
    172. Catch ex As Exception
    173. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    174. End Try
    175. End Sub
    176. End Class


    CodeTags korrigiert
    Thema verschoben; Das Thema wird automatisch dort erstellt, wo man sich befindet, wenn man auf [* Neues Thema] klickt.
    ~VaporiZed

    :thumbsup:

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    Hier der Weg ohne Umweg über HTML. (Automatisieren von Microsoft Word zum Erstellen eines neuen Dokuments mithilfe von Visual Basic)
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub ExportZuWordToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportZuWordToolStripMenuItem.Click
    2. Try
    3. Cursor = Cursors.WaitCursor
    4. 'Request a file name to save to
    5. Dim sfd As New SaveFileDialog()
    6. With sfd
    7. .FileName = "Export"
    8. .Title = "Export to *.docx File"
    9. .Filter = "Word document (*.docx)|*.docx"
    10. .OverwritePrompt = True
    11. If .ShowDialog = Windows.Forms.DialogResult.OK Then
    12. Speichername = .FileName
    13. Else
    14. Cursor = Cursors.Hand
    15. Exit Sub
    16. End If
    17. End With
    18. Dim oWord As Word.Application
    19. Dim oDoc As Word.Document
    20. Dim oPara1 As Word.Paragraph
    21. Dim oPara2 As Word.Paragraph
    22. Dim treeContents As New System.Text.StringBuilder
    23. Dim treeEnumerator As IEnumerator = TreeView1.Nodes.GetEnumerator
    24. 'Export the tree view to the treeContents string builder
    25. Do Until treeEnumerator.MoveNext = False
    26. 'Pass the current root node to the OutputTreeNodesToString routine.
    27. OutputTreeNodesToString(DirectCast(treeEnumerator.Current, TreeNode), 0, treeContents)
    28. Loop
    29. oWord = New Word.Application
    30. oWord.Visible = True
    31. oDoc = oWord.Documents.Add
    32. With oDoc
    33. 'Absatz am Anfang des Dokuments einfügen.
    34. oPara1 = oDoc.Content.Paragraphs.Add
    35. oPara1.Range.Text = "Tree View Export"
    36. oPara1.Range.Font.Name = "Arial"
    37. oPara1.Range.Font.Size = 24
    38. oPara1.Range.Font.Bold = 1 'MsoTriState.msoTrue
    39. oPara1.Range.InsertParagraphAfter()
    40. 'Absatz am Ende des Dokuments einfügen.
    41. oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
    42. oPara1.Range.Font.Name = "Arial"
    43. oPara2.Range.Font.Size = 10
    44. oPara2.Range.Font.Bold = 1
    45. oPara2.Range.Text = treeContents.ToString
    46. oPara2.Range.InsertParagraphAfter()
    47. End With
    48. 'speichern
    49. oDoc.SaveAs(Speichername.ToString)
    50. oDoc = Nothing
    51. oWord.Quit()
    52. Cursor = Cursors.Hand
    53. MessageBox.Show("Export nach " & Speichername & " abgeschlossen.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
    54. Catch ex As Exception
    55. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    56. End Try
    57. End Sub
    58. Private Sub OutputTreeNodesToString(ByVal currentNode As TreeNode, ByVal nodeDepth As Int32, ByRef treeContentsStringBuilder As System.Text.StringBuilder)
    59. Try
    60. If currentNode IsNot Nothing Then
    61. 'Retrieve an enumerator for the current node.
    62. Dim nodeEnumerator As IEnumerator = currentNode.Nodes.GetEnumerator
    63. 'Write the current node to the string builder using no breaking spaces (&nbsp;) based on the nodeDepth value.
    64. For i As Int32 = 0 To nodeDepth
    65. treeContentsStringBuilder.Append(" ")
    66. Next
    67. treeContentsStringBuilder.AppendLine(New String(Tab, nodeDepth) & currentNode.Text)
    68. 'If the currentNode has child nodes, increment the nodeDepth before recalling this routine.
    69. If currentNode.Nodes.Count > 0 Then nodeDepth += 1
    70. Do Until nodeEnumerator.MoveNext = False
    71. OutputTreeNodesToString(DirectCast(nodeEnumerator.Current, TreeNode), nodeDepth, treeContentsStringBuilder)
    72. Loop
    73. End If
    74. Catch ex As Exception
    75. MessageBox.Show("Fehler: " & ex.ToString, "Fehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    76. End Try
    77. End Sub