Hello all.
I need to write an export function that will create a .csv file and place it on the users PC. Creating the data is not a problem, but how to write it to a file (maybe the downloads folder on a Windows machine - not sure the equivalent on a MAC or Linux machine).
Can anyone point me in the right direction?
Thank you,
Tim
I wrote a simple csv file editing app for the Just Code Challenge last year. The project is here:
http://electronbunker.ca/XojoProjects/Week5_CSV_Editor.zip
Have a look at the SaveFile, and OutputCSV routines.
Edit: Sorry, I just noticed that you’re dealing with a web application. I don’t know whether this would be of any use.
1 Like
Thanks for the effort Robert!
I’ll have a look but I am 99.9% sure that web is a different animal…
Tim
WebFile
.forcedownload = true
Then showURL
check out the video here, from about 7 minutes in…
https://www.youtube.com/watch?v=vbamS7wujKE
Here are the functions you search:
Recordset from Database to CSV:
Public Function RStoCSV(Data as RecordSet, Optional Delemiter as String = ";") as String
dim CSV() as String
'Header
dim head() as string
for i as integer = 1 to data.FieldCount
head.Append(data.IdxField(i).Name )
next i
CSV.Append(Join(head, Delemiter))
'content
While Not data.EOF
dim row() as string
for i as Integer = 1 to data.FieldCount
dim inhalt as string = data.IdxField(i).StringValue.DefineEncoding(Encodings.UTF8)
inhalt = ReplaceAll(inhalt, chr(10), " | ")
inhalt = ReplaceAll(inhalt, chr(13), "")
inhalt = ReplaceAll(inhalt, chr(9), " ")
inhalt = ReplaceAll(inhalt, Delemiter, " ")
inhalt = ReplaceAll(inhalt, """", "\""")
row.Append(inhalt)
next i
CSV.Append(Join(row, Delemiter))
data.MoveNext
Wend
data.MoveFirst
dim outp as string = Join(CSV, EndOfLine)
outp = ReplaceAll(outp, chr(10), "")
outp = ReplaceAll(outp, EndOfLine, "")
return Join(CSV, EndOfLine)
End Function
Download the File:
-
place a property into the app object:
Public Property TextFile as WebFile
-
then add this function to the app object as well:
Public Sub DownloadFile(Content as String, Filename as string, optional Format as string = "csv", optional enc as string = "utf8")
TextFile = New WebFile // TextFile is a property of the web page
TextFile.MimeType = "text/plain"
TextFile.ForceDownload = True // If False, the browser may try to display the file instead of download it
TextFile.FileName = Filename + "." + Format
TextFile.Data = ConvertEncoding(Content, Encodings.UTF8)
// use different coding if wanted
if enc = "ascii" then TextFile.Data = ConvertEncoding(Content, Encodings.ASCII)
if enc = "ansi" and Session.Platform = WebSession.PlatformType.Windows then TextFile.Data = ConvertEncoding(Content, Encodings.WindowsANSI)
webpage1.ShowURL(TextFile.URL) // This causes the file to be downloaded
End Sub
To download the file after you’ve generated the CSV, put the data in a WebFile and then use ShowURL
to begin the download.
Edit: I see Jeff beat me to it!