Dynamically creating .xls file.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings,

I’ve query some data from the table, how can I push it into an excel file and allow user to save it? anyone done this sort of thing before?
 
Select Case (svlType.ToLower)
Case "txt" : svlType = "text/plain"
Case "htm" : svlType = "text/HTML"
Case "html" : svlType = "text/HTML"
Case "pdf" : svlType = "Application/pdf"
Case "gif" : svlType = "image/GIF"
Case "jpg" : svlType = "image/JPEG"
Case "jpeg" : svlType = "image/JPEG"
Case "png" : svlType = "image/png"
Case "doc" : svlType = "Application/msword" ' (for Microsoft Word files)
Case "xls" : svlType = "Application/x-msexcel" '(for Microsoft Excel files)
End Select

HttpContext.Current.Response.ContentType =svlType
HttpContext.Current.Response.write(YourVariable)

YourVariable is the variable which holds data.

In case while saving if you expect the file to be saved in a filename you specify You can use

HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" & myFileName)


In the case with FileName, use it as follows:


HttpContext.Current.Response.ContentType =svlType
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=" & myFileName)
HttpContext.Current.Response.write(YourVariable)
 
if do like this, can the client application open the file correctly. PDF,
gif, jpeg etc. are all not plain text files, word, excel needless to say.

In my opinion, we should try to use COM+ or some other ways to generate xls
file, and send back to client. in this newsgroup, there are a lot of
threads on this topic.
Select Case (svlType.ToLower)
Case "txt" : svlType = "text/plain"
Case "htm" : svlType = "text/HTML"
Case "html" : svlType = "text/HTML"
Case "pdf" : svlType = "Application/pdf"
Case "gif" : svlType = "image/GIF"
Case "jpg" : svlType = "image/JPEG"
Case "jpeg" : svlType = "image/JPEG"
Case "png" : svlType = "image/png"
Case "doc" : svlType = "Application/msword" ' (for Microsoft Word files)
Case "xls" : svlType = "Application/x-msexcel" '(for Microsoft Excel files)
End Select

HttpContext.Current.Response.ContentType =svlType
HttpContext.Current.Response.write(YourVariable)

YourVariable is the variable which holds data.

In case while saving if you expect the file to be saved in a
filename you specify You can use
HttpContext.Current.Response.AppendHeader("Content-disposition",
"attachment; filename=" & myFileName)
In the case with FileName, use it as follows:


HttpContext.Current.Response.ContentType =svlType
HttpContext.Current.Response.AppendHeader("Content-disposition",
"attachment; filename=" & myFileName)
 
Back
Top