-
Feb 25th, 2017, 03:51 AM
#1
Thread Starter
Lively Member
Show SaveFileDialog for user to save and export file
Greetings all,
I have this code which works this way:
When user click EXPORT, csv file is created at the Root Folder.
However I want to be able to have the SaveDialog box pop-up, that allows user to enter a name for the file and save to wherever they want in computer.
How can I modify the code to do so?
Code:
Private Sub export_btn_Click(sender As Object, e As EventArgs) Handles export_btn.Click
Dim exportFileName As String = "EVC_kiosk_DB_" & Date.Now.ToString("dd-MMM-yyyy") & ".csv"
Try
Dim fs As New IO.StreamWriter(".\" & exportFileName, False)
fs.WriteLine("<?xml version=""1.0""?>")
fs.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
fs.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
fs.WriteLine(" <ss:Styles>")
fs.WriteLine(" <ss:Style ss:ID=""1"">")
fs.WriteLine(" <ss:Font ss:Bold=""1""/>")
fs.WriteLine(" </ss:Style>")
fs.WriteLine(" </ss:Styles>")
fs.WriteLine(" <ss:Worksheet ss:Name=""Sheet1"">")
fs.WriteLine(" <ss:Table>")
For x As Integer = 0 To ProjectsDataGridView.Columns.Count - 1
fs.WriteLine("<ss:Column ss:Width=""{0}""/>",
ProjectsDataGridView.Columns.Item(x).Width)
Next
fs.WriteLine(" <ss:Row ss:StyleID=""1"">")
For i As Integer = 0 To ProjectsDataGridView.Columns.Count - 1
fs.WriteLine(" <ss:Cell>")
fs.WriteLine(String.Format(
" <ss:Data ss:Type=""String"">{0}</ss:Data>",
ProjectsDataGridView.Columns.Item(i).HeaderText))
fs.WriteLine(" </ss:Cell>")
Next
fs.WriteLine(" </ss:Row>")
For intRow As Integer = 0 To ProjectsDataGridView.RowCount - 1
fs.WriteLine(String.Format("<ss:Row ss:Height =""{0}"">",
ProjectsDataGridView.Rows(intRow).Height))
For intCol As Integer = 0 To ProjectsDataGridView.Columns.Count - 1
fs.WriteLine(" <ss:Cell>")
fs.WriteLine(String.Format(
" <ss:Data ss:Type=""String"">{0}</ss:Data>",
ProjectsDataGridView.Item(intCol, intRow).Value.ToString))
fs.WriteLine(" </ss:Cell>")
Next
fs.WriteLine(" </ss:Row>")
Next
fs.WriteLine(" </ss:Table>")
fs.WriteLine(" </ss:Worksheet>")
fs.WriteLine("</ss:Workbook>")
fs.Close()
MessageBox.Show(exportFileName & " has been exported to your root folder.")
Catch ex As Exception
MessageBox.Show("Error exporting data! Please ensure that the excel file is not opened.")
End Try
End Sub
-
Feb 25th, 2017, 06:53 AM
#2
Re: Show SaveFileDialog for user to save and export file
Do you want to let the user select the name of the file or just the location of the folder that the file will be saved to? If it's the latter then you would use a FolderBrowserDialog rather than a SaveFileDialog. You would then construct your file name the same way but append it to the path from the SelectedPath of the FolderBrowserDialog, e.g.
vb.net Code:
Using dialogue As New FolderBrowserDialog If dialogue.ShowDialog() = DialogResult.OK Then Dim exportFileName As String = "EVC_kiosk_DB_" & Date.Now.ToString("dd-MMM-yyyy") & ".csv" Dim exportFilePath As String = IO.Path.Combine(dialogue.SelectedPath, exportFileName) Try 'Use exportFilePath here. Catch ex As Exception End Try End If End Using
I have two points though.
1. When incorporating dates and times into file and folder paths, I strongly recommend going from most significant to least significant. That would mean using "yyyy-MMM-dd" or the like as the date format. The reason is that then alphabetical and chronological order will match.
2. Why are you giving your file a ".csv" extension when it's not a CSV file? That looks rather like you're actually creating an XLSX file, in which case you should give it a ".xlsx" extension. I'd also recommend using the Open XML SDK to write Office XML files rather than writing them as text.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|