Textfile in Excel exportieren

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von lordknoblauch.

    Textfile in Excel exportieren

    Hallo Zusammen
    Hat das jemand von euch schon gemacht?
    Ich habe ein Textfile mit mehreren Zeilen, die Wörter in einer Linie sind jeweils mit einem Semikolon (;) getrennt.
    Nun möchte ich dieses Textfile in ein Excel exportieren, aber so, dass nach jedem Semikolon eine neue Spalte genommen wird.

    Geht das?

    Gruss und Danke
    reustli
    als lösungsansatz:
    DatagridViewToExcel

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Windows.Forms
    3. Public Class Class1
    4. Public Sub exportExcel(ByVal grdView As DataGridView, ByVal fileName As String)
    5. ' Choose the path, name, and extension for the Excel file
    6. Dim myFile As String = fileName
    7. ' Open the file and write the headers
    8. Dim fs As New IO.StreamWriter(myFile, False)
    9. fs.WriteLine("<?xml version=""1.0""?>")
    10. fs.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
    11. fs.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
    12. ' Create the styles for the worksheet
    13. fs.WriteLine(" <ss:Styles>")
    14. ' Style for the column headers
    15. fs.WriteLine("<ss:Style ss:ID=""1"">")
    16. fs.WriteLine("<ss:Font ss:Bold=""1""/>")
    17. fs.WriteLine("<ss:Alignment ss:Horizontal=""Center"" ss:Vertical=""Center"" " & _
    18. "ss:WrapText=""1""/>")
    19. fs.WriteLine("<ss:Interior ss:Color=""#C0C0C0"" ss:Pattern=""Solid""/>")
    20. fs.WriteLine("</ss:Style>")
    21. ' Style for the column information
    22. fs.WriteLine("<ss:Style ss:ID=""2"">")
    23. fs.WriteLine("<ss:Alignment ss:Vertical=""Center"" ss:WrapText=""1""/>")
    24. fs.WriteLine("</ss:Style>")
    25. fs.WriteLine("</ss:Styles>")
    26. ' Write the worksheet contents
    27. fs.WriteLine("<ss:Worksheet ss:Name=""Sheet1"">")
    28. fs.WriteLine("<ss:Table>")
    29. For i As Integer = 0 To grdView.Columns.Count - 1
    30. fs.WriteLine(String.Format("<ss:Column ss:Width=""{0}""/>", _
    31. grdView.Columns.Item(i).Width))
    32. Next
    33. fs.WriteLine("<ss:Row>")
    34. For i As Integer = 0 To grdView.Columns.Count - 1
    35. fs.WriteLine(String.Format("<ss:Cell ss:StyleID=""1"">" & _
    36. "<ss:Data ss:Type=""String"">{0}</ss:Data></ss:Cell>", _
    37. grdView.Columns.Item(i).HeaderText))
    38. Next
    39. fs.WriteLine("</ss:Row>")
    40. ' Check for an empty row at the end due to Adding allowed on the DataGridView
    41. Dim subtractBy As Integer, cellText As String
    42. If grdView.AllowUserToAddRows = True Then subtractBy = 2 Else subtractBy = 1
    43. ' Write contents for each cell
    44. For i As Integer = 0 To grdView.RowCount - subtractBy
    45. fs.WriteLine(String.Format("<ss:Row ss:Height=""{0}"">", _
    46. grdView.Rows(i).Height))
    47. For intCol As Integer = 0 To grdView.Columns.Count - 1
    48. cellText = grdView.Item(intCol, i).Value
    49. ' Check for null cell and change it to empty to avoid error
    50. If cellText = vbNullString Then cellText = ""
    51. fs.WriteLine(String.Format("<ss:Cell ss:StyleID=""2"">" & _
    52. "<ss:Data ss:Type=""String"">{0}</ss:Data></ss:Cell>", _
    53. cellText.ToString))
    54. Next
    55. fs.WriteLine("</ss:Row>")
    56. Next
    57. ' Close up the document
    58. fs.WriteLine("</ss:Table>")
    59. fs.WriteLine("</ss:Worksheet>")
    60. fs.WriteLine("</ss:Workbook>")
    61. fs.Close()
    62. End Sub
    63. Sub ToXLS(ByVal DG As DataGridView, Optional ByVal Standardname As String = "")
    64. Dim filesave As SaveFileDialog = New SaveFileDialog
    65. filesave.AddExtension = True
    66. filesave.Filter = "Excel-Dateien (*.xls)|*.xls"
    67. filesave.FileName = Standardname
    68. If filesave.ShowDialog = DialogResult.Cancel Then
    69. GoTo ende
    70. End If
    71. On Error GoTo break
    72. exportExcel(DG, filesave.FileName)
    73. MsgBox("Datei wurde angelegt: " & filesave.FileName)
    74. GoTo ende
    75. break:
    76. errormessage()
    77. ende:
    78. End Sub
    79. Sub errormessage()
    80. MessageBox.Show("Exportieren der Datei fehlgeschlagen!", "Exportfehler!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    81. End Sub
    82. End Class

    ist halt vorgesehen um ein DataGridView nach excel zu exportieren, aber bietet schon einen dicken ansatz