Sprache: VB
Gibt ein beliebiges Gridview als Excelfile aus.
Sub GridView2CSV(ByVal myGridview As GridView, ByVal File As String)
''// Get the Columns of the Gridview
Dim ColumnsCount As Integer = myGridview.Columns.Count - 1
''// Get and Write the HeaderNames
Dim HeaderNames As String = Nothing
For i = 0 To ColumnsCount
HeaderNames += myGridview.Columns(i).HeaderText & ";"
Next
CW(HeaderNames, File)
''// Get the Rows Content
Dim RowValue As String = Nothing
For Each myRow As GridViewRow In myGridview.Rows
For i = 0 To ColumnsCount
RowValue += myRow.Cells(i).Text & ";"
Next
CW(RowValue, File)
RowValue = Nothing
Next
End Sub
Sub CW(ByVal Text, ByVal FileName)
Dim myFileWriter As New System.IO.StreamWriter(FileName, True)
myFileWriter.WriteLine(Text)
myFileWriter.Close()
End Sub
End Class
Sub GridView2CSV(ByVal myGridview As GridView, ByVal File As String)
''// Get the Columns of the Gridview
Dim ColumnsCount As Integer = myGridview.Columns.Count - 1
''// Get and Write the HeaderNames
Dim HeaderNames As String = Nothing
For i = 0 To ColumnsCount
HeaderNames += myGridview.Columns(i).HeaderText & ";"
Next
CW(HeaderNames, File)
''// Get the Rows Content
Dim RowValue As String = Nothing
For Each myRow As GridViewRow In myGridview.Rows
For i = 0 To ColumnsCount
RowValue += myRow.Cells(i).Text & ";"
Next
CW(RowValue, File)
RowValue = Nothing
Next
End Sub
Sub CW(ByVal Text, ByVal FileName)
Dim myFileWriter As New System.IO.StreamWriter(FileName, True)
myFileWriter.WriteLine(Text)
myFileWriter.Close()
End Sub
End Class
Alte URL:
/snippet/asp-net-gridview-to-excel/1112