Public Function WriteToCSV(ByVal dataTable As DataTable, ByVal filePath As String, ByVal records As ArrayList) As Boolean Dim fileStream As System.IO.FileStream Dim streamReader As System.IO.StreamReader Dim streamWriter As System.IO.StreamWriter Dim i, j As Integer Dim strRow As String Try If (System.IO.File.Exists(filePath)) Then System.IO.File.Delete(filePath) End If fileStream = New FileStream(filePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write) If Not dataTable Is Nothing Then streamWriter = New StreamWriter(fileStream, System.Text.Encoding.Default) For i = 0 To dataTable.Rows.Count - 1 strRow = "" For j = 0 To dataTable.Columns.Count - 1 strRow += dataTable(i)(j) If j < dataTable.Columns.Count - 1 Then strRow += "," Else Next streamWriter.WriteLine(strRow) Next streamWriter.Close() End If fileStream.Close() Return True Catch ex As Exception Return False End Try End Function