PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Show SaveFileDialog for user to save and export file-VBForums
Results 1 to 2 of 2

Thread: Show SaveFileDialog for user to save and export file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    69

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,566

    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:
    1. Using dialogue As New FolderBrowserDialog
    2.     If dialogue.ShowDialog() = DialogResult.OK Then
    3.         Dim exportFileName As String = "EVC_kiosk_DB_" & Date.Now.ToString("dd-MMM-yyyy") & ".csv"
    4.         Dim exportFilePath As String = IO.Path.Combine(dialogue.SelectedPath, exportFileName)
    5.  
    6.         Try
    7.             'Use exportFilePath here.
    8.         Catch ex As Exception
    9.  
    10.         End Try
    11.     End If
    12. 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
  •  



Featured


Click Here to Expand Forum to Full Width