Dim saveFileDialog As New Microsoft.Win32.SaveFileDialog
saveFileDialog.DefaultExt = "txt"
Dim reportFileName As String = "ClientGroupExport_" & System.DateTime.Now.ToString("yyyyMMdd")
Dim currExt = IO.Path.GetExtension(reportFileName)
saveFileDialog.FileName = reportFileName
Try
If saveFileDialog.ShowDialog() Then
'SavedFileName = saveFileDialog.FileName
Using fileStream As System.IO.Stream = saveFileDialog.OpenFile()
Using sw As New System.IO.StreamWriter(fileStream)
'Me.FilteredClientGroups.CopyTo(fileStream)
Dim str As String = String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\newLN", "ClientType", "SimpleName", "SingleLineAddress", "Phone", "Fax", "Email", "Mobile", "Owner", "ClientSourceType").Replace("\t", vbTab).Replace("\newLN", vbNewLine)
str = str & String.Join(Environment.NewLine, FilteredClientGroupMembers.Select(Of String)(Function(x) String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}", x.ClientType, x.SimpleName, x.SingleLineAddress, x.Phone, x.Fax, x.Email, x.Mobile, x.Owner, x.ClientSourceType))).Replace("\t", vbTab)
sw.WriteLine(str)
sw.Flush()
sw.Close()
End Using
End Using
If IO.File.Exists(saveFileDialog.FileName) Then
If Windows.MessageBox.Show("Do you wish to open the saved file now?", "Open File?", Windows.MessageBoxButton.YesNo, Windows.MessageBoxImage.Question, Windows.MessageBoxResult.No) = Windows.MessageBoxResult.Yes Then
'Open the file in the default viewer
Try
System.Diagnostics.Process.Start(saveFileDialog.FileName)
Catch ex As Exception
Windows.MessageBox.Show(String.Format("An error occurred trying to open the file '{0}'", saveFileDialog.FileName), "File Error", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
RollbarClientSync.SendExceptionAsync(ex, Controller.CurrentIdentity)
End Try
End If
End If
End If
Catch ioEx As System.IO.IOException
Windows.MessageBox.Show(String.Format("There was a problem saving the file to '{0}'. The error was: {1}{2}Check to see if the file is already in use, and that you have permissions to write to the requested file path.", saveFileDialog.FileName, ioEx.GetBaseException.Message, Environment.NewLine), "Failed To Save Report", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
Catch fileEx As Exception
Windows.MessageBox.Show(String.Format("There was a problem saving the file to '{0}'. The error was: {1}", saveFileDialog.FileName, fileEx.GetBaseException.Message), "Failed To Save Client Group report", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
RollbarClientSync.SendExceptionAsync(fileEx, Controller.CurrentIdentity)
End Try
''OR
Dim saveFileDialog As New Microsoft.Win32.SaveFileDialog
saveFileDialog.DefaultExt = "tab"
Dim reportFileName As String = "ClientGroupExport_" & System.DateTime.Now.ToString("yyyyMMdd")
Dim currExt = IO.Path.GetExtension(reportFileName)
saveFileDialog.FileName = reportFileName
Try
If saveFileDialog.ShowDialog() Then
'SavedFileName = saveFileDialog.FileName
Using fileStream As System.IO.Stream = saveFileDialog.OpenFile()
Using sw As New System.IO.StreamWriter(fileStream)
'Me.FilteredClientGroups.CopyTo(fileStream)
Dim lines As New List(Of String)
Dim headers As New List(Of String)
headers.Add("ClientType")
headers.Add("SimpleName")
headers.Add("SingleLineAddress")
headers.Add("Phone")
headers.Add("Fax")
headers.Add("Email")
headers.Add("Mobile")
headers.Add("ContactName")
headers.Add("ContactEmail")
headers.Add("ContactPhone")
headers.Add("Owner")
headers.Add("ClientSourceType")
lines.Add(String.Join(vbTab, headers.ToArray))
For Each row In FilteredClientGroupMembers
Dim elements As New List(Of String)
elements.Add(row.ClientType)
elements.Add(row.SimpleName)
elements.Add(row.SingleLineAddress)
elements.Add(row.Phone)
elements.Add(row.Fax)
elements.Add(row.Email)
elements.Add(row.Mobile)
elements.Add(row.ContactName)
elements.Add(row.ContactEmail)
elements.Add(row.ContactPhone)
elements.Add(row.Owner)
elements.Add(row.ClientSourceType)
lines.Add(String.Join(vbTab, elements.ToArray))
Next
''Dim str As String = String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\newLN", "ClientType", "SimpleName", "SingleLineAddress", "Phone", "Fax", "Email", "Mobile", "Owner", "ClientSourceType").Replace("\t", vbTab).Replace("\newLN", vbNewLine)
'Str = str & String.Join(Environment.NewLine, FilteredClientGroupMembers.Select(Of String)(Function(x) String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}", x.ClientType, x.SimpleName, x.SingleLineAddress, x.Phone, x.Fax, x.Email, x.Mobile, x.Owner, x.ClientSourceType))).Replace("\t", vbTab)
Dim contents = String.Join(Environment.NewLine, lines.ToArray)
sw.WriteLine(contents)
sw.Flush()
sw.Close()
End Using
End Using
If IO.File.Exists(saveFileDialog.FileName) Then
If Windows.MessageBox.Show("Do you wish to open the saved file now?", "Open File?", Windows.MessageBoxButton.YesNo, Windows.MessageBoxImage.Question, Windows.MessageBoxResult.No) = Windows.MessageBoxResult.Yes Then
'Open the file in the default viewer
Try
System.Diagnostics.Process.Start(saveFileDialog.FileName)
Catch ex As Exception
Windows.MessageBox.Show(String.Format("An error occurred trying to open the file '{0}'", saveFileDialog.FileName), "File Error", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
RollbarClientSync.SendExceptionAsync(ex, Controller.CurrentIdentity)
End Try
End If
End If
End If
Catch ioEx As System.IO.IOException
Windows.MessageBox.Show(String.Format("There was a problem saving the file to '{0}'. The error was: {1}{2}Check to see if the file is already in use, and that you have permissions to write to the requested file path.", saveFileDialog.FileName, ioEx.GetBaseException.Message, Environment.NewLine), "Failed To Save Report", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
Catch fileEx As Exception
Windows.MessageBox.Show(String.Format("There was a problem saving the file to '{0}'. The error was: {1}", saveFileDialog.FileName, fileEx.GetBaseException.Message), "Failed To Save Client Group report", Windows.MessageBoxButton.OK, Windows.MessageBoxImage.Error)
RollbarClientSync.SendExceptionAsync(fileEx, Controller.CurrentIdentity)
End Try