Solved

.CSV File Download tries to save as .XLS

Posted on 2011-03-03
6
399 Views
Last Modified: 2012-06-21
Hi,

I have an application that creates a .CSV. The problem I have is when the user clicks on the link location it opens a File Download dialog that gives the options to Save, Open, Cancel. If you choose save it saves as a .XLS, and obviously the formatting is all screwed up when you open it. If you choose open, and then try to save the file it at leasts has .CSV as the save as type. Is there any way around this?
Private Sub CreateDualEnrollmentFile()
        ' =========================================================
        ' Create file 
        ' =========================================================
        Dim Myconn As SqlConnection
        Dim Mycomm As SqlCommand
        Dim dtr As SqlDataReader
        Dim dadapter As SqlDataAdapter
        Dim ds As DataSet
        Dim drow As DataRow
        Dim vStamp As DateTime = Now()
        Dim vStampForm = vStamp.ToString.Format("{0:MM-dd-yy_HH_mm_ss_tt}", DateTime.Now)

        Dim sql
        sql = "SELECT * FROM NSU_Dual_Enrollment_Application WHERE ([Stamp] > '" & gLastDate & "') ORDER BY Stamp ;"
       
        Dim vFileBody
        Myconn = New SqlConnection(ConfigurationSettings.AppSettings("gDataSource"))
        Mycomm = New SqlCommand(sql, Myconn)
        dadapter = New SqlDataAdapter
        dadapter.SelectCommand = Mycomm
        ds = New DataSet
        dadapter.Fill(ds, "NSU_Dual_Enrollment_Application")
        vFileBody = vFileBody & "FirstName" & "," & "MiddleName" & "," & "LastName" & "," & "SSN" & "," & "DOB" & "," & "Street" & "," & "City" & "," & "State" & ","
        vFileBody = vFileBody & "Zip" & "," & "Phone" & "," & "CellPhone" & "," & "Email" & ","
        vFileBody = vFileBody & "HighSchool" & "," & "HSGradDate" & "," & "PrevCollege" & ","
        vFileBody = vFileBody & "IntendedMajor" & "," & "Comments" & "," & "Stamp" & "," & vbCrLf
        If ds.Tables("NSU_Dual_Enrollment_Application").Rows.Count = 0 Then
            lblTextFile.Text = "No new applications were found."
        Else

            For Each drow In ds.Tables("NSU_Dual_Enrollment_Application").Rows
                vFileBody = vFileBody & Chr(34) & drow("FirstName") & Chr(34) & "," & Chr(34) & drow("MiddleName") & Chr(34) & "," & Chr(34) & drow("lastName") & Chr(34) & ","
                vFileBody = vFileBody & drow("SSN") & "," & drow("DOB") & "," & Chr(34) & drow("Street") & Chr(34) & "," & Chr(34) & drow("City") & Chr(34) & "," & Chr(34) & drow("State") & Chr(34) & "," & Chr(34) & drow("Zip") & Chr(34) & ","
                vFileBody = vFileBody & drow("Phone") & "," & drow("CellPhone") & "," & drow("Email") & "," & Chr(34) & drow("HighSchool") & Chr(34) & ","
                vFileBody = vFileBody & Chr(34) & drow("HSGradDate") & Chr(34) & "," & Chr(34) & drow("PrevCollege") & Chr(34) & "," & Chr(34) & drow("IntendedMajor") & Chr(34) & "," & Chr(34) & drow("Comments") & Chr(34) & ","
                vFileBody = vFileBody & drow("Stamp") & "," & vbCrLf
            Next

            Myconn.Close()
           
            'Create and save info in a text file.
            Dim vFileName = "EnrollmentData/DualEnrollmentReport_" & vStampForm & ".csv"
            
            '& Now() & "_" & "Scholarship_Report.txt"
            Try
                Dim FILENAME As String = Server.MapPath(vFileName)
                Dim objStreamWriter As StreamWriter
                objStreamWriter = File.CreateText(FILENAME)
                objStreamWriter.WriteLine(vFileBody)
                objStreamWriter.Close()            
                lblTextFile.Visible = True
                lblTextFile.Text = "File written and located <a href='" & vFileName & "'TARGET=_BLANK>here</a>."
                UpdateAdmissionsLastDateRan()
                btnText.Visible = False
            Catch Ex As Exception           
                lblTextFile.Visible = True
                lblTextFile.Text = "An error occurred attempting to produce the text file." & Ex.Message & sql             
            End Try
        End If
    End Sub

Open in new window

0
Comment
Question by:savache27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 400 total points
ID: 35028147
As I understand it,  your code is correct and produces a well formed CSV file.

The problem is on the client machine.   It sounds as if Excel is the default application for CSV files and is therefore getting involved in the save operation.

I don't think there is anything you can do in your application to affect this behaviour.

The only solution I can see (without requiring changes on the client machines)  is to use a .TXT  extension for your file to take Excel out of the equation.  
0
 

Author Comment

by:savache27
ID: 35028513
Thanks for looking at it. Would it be a huge pain to format this into a .XLS file? They can use either, but I'm assuming it would be a major code change and I don't really know anything about creating an Excel file.
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 100 total points
ID: 35028648
If you convert the commas to tabs and save as a .txt file, it will also be readable by Excel and most other applications. That is a standard format that everyone will recognize.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35028650
Tab is chr(9) by the way.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35028697
If you want to write to an xls file, the easiest way would be to reference the Excel library and create it there. That would require that the end user has Excel.
0
 

Author Closing Comment

by:savache27
ID: 35037440
Thank you so much for the help! It was greatly appreciated.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question