Classic ASP Generate xls or csv with readonly fields - asp-classic

At the top of my page I have the code
<%
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "attachment;filename=myfile.xls"%>
which forces my table to download as a spreadsheet. But, how can I make certain cells of this download read only ?

Related

ASP.NET how to return pdf file as response without changing response header

I want to return a pdf file as response to some button click.
I succeeded to send the pdf file, but when i try to save it via the browser, it won't let me save it as a .pdf file (but as .aspx file)
here's the code:
Dim myWebClient As WebClient = New WebClient()
Dim myDataBuffer As Byte() = myWebClient.DownloadData(LocalImageURL) ' LocalImageURL is some path to a pdf file
Response.ContentType = "application/pdf"
Response.BinaryWrite(myDataBuffer)
Response.Flush()
Response.End()
if I am adding also the following line before writing the byte array:
Response.AddHeader("content-disposition", "attachment;filename=report.pdf")
it does the trick, but the problem is that the page remains stuck (looks like it still waits for server response to come)
This has been asked and answered before. Details are at the link below:
asp.net VB.net file download handler not work properly

Generating and downloading an `Excel` document breaks `ASP.net` LoginStatus

In my ASP.net web application I have a page which shows a (not very complicated) table. I have implemented a button so when the (registered) members clicks on it, the browser downloads the table as an Excel document
The code to generate this Excel is as follow:
Dim response As HttpResponse = HttpContext.Current.Response
' first let's clean up the response.object
response.Clear()
response.Charset = ""
Dim filename As String = MapPath("~/ex1.xls")
' set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")
' create a string writer
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
' instantiate a datagrid
Dim dg As New DataGrid()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.[End]()
End Using
End Using
The problem is that this code is breaking somehow the ASPN.net LoginStatus as when you try to log out after downloading this Excel (the "log out" buttom is always visible at the application) every click on the buttom will download the same Excelover and over (once per click). If you reload window or change page the "log out" will work again.
this is the "log out" link when inspecting it with chrome:
<a id="ctl00_ctl00_MainContent_LoginStatus1" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LoginStatus1$ctl00','')">Log Out</a>
I have tried to make the Excel to download from other page using:
onclick="window.document.forms[0].target='_blank';"
and now it opens a new tab, download the Excel, close the tab (fast process) but still keeps breaking the logout link.
And I run out of ideas, any help, code, hint or whatever possible solution to fix this problem is very welcome.
Open a new page within the blank target, and let the new page generate the excel
This looks like a server side error.
Your code to logout actually runs the code to download Excel.
May be you should throw in a 'Response.End();' in the click handler for logout.
Well a quick work around would be to use report viewer control, which has an export to excel functionality.
I know this solution is not exactly what you were looking for, but from my experience when a user is looking for excel functionality, instead of providing a table/gridview results to the user i provide an rdlc report, which has all the export functionality.

Writing table contents to xls from vb.net

I have a program with an option to write db contents to an excel spreadsheet. This functionality is contained on a page called 'Search_aspx'.
Response.ClearContent()
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=FileName.xls")
Response.Cache.SetCacheability(HttpCacheability.Private)
Dim dg As New DataGrid
dg.DataSource = dtResults
dg.DataBind()
Dim sw As New System.IO.StringWriter()
Dim htw As New HtmlTextWriter(sw)
dg.RenderControl(htw)
Response.Write(sw.ToString)
Response.Flush()
Response.Close()
This worked fine forever, but last week I migrated the solution to a new server (Server03 to Server08), and now it is not working. When I try to save, it asks if I want to "Save search_aspx" instead of "Save FileName.xls".
What is the issue here?
EDIT:
All righty... I took the advice posted below and followed Mason's suggestion, and the page still asks if I want to save 'Search_aspx'. Now have EPPlus installed in my solution, properly referenced etc....
Response.ClearContent()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("Content-Disposition", "attachment; filename=FileName.xlsx")
Dim package As New ExcelPackage()
Dim sheet = package.Workbook.Worksheets.Add("Data")
sheet.Cells.LoadFromDataTable(dtResults, False)
Response.BinaryWrite(package.GetAsByteArray())
Response.Flush()
Response.Close()
The real issue is you aren't creating an XLS file. You're creating an HTML file with a .xls extension. I discuss several reasons why that's a bad idea and go into detail about what you can do instead on my blog.
The short and sweet version is that there's no good way practice to generate a .xls file in an ASP.NET environment, and you should either generate .xlsx files with a decent library (discussed on my blog) or switch to another data format such as CSV.

ASP Classic download file script

I have a website being built in ASP Classic, and am having some trouble with a script that allows a user to download a file, but hides the path to the file.
When a user is on a page, they will see a link. The link is coded like this:
Download File
This link takes them to download.asp where code is run to get the file and deliver it. Here is the code I have right now:
<%
const adTypeBinary = 1
dim strFilePath, strFile
strFilePath = "/_uploads/private/"
strFile = Request.QueryString("file")
if strFile <> "" then
'Set the content type to the specific type that you are sending.
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile
set objStream = Server.CreateObject("ADODB.Stream")
objStream.open
objStream.type = adTypeBinary
objStream.LoadFromFile(strFilePath & strFile)
response.binarywrite objStream.Read
objStream.close
Set objStream = nothing
end if
%>
This code I put together from both questions on this site (How to download the files using vbscript in classic asp), and from http://support.microsoft.com/kb/276488
What is happening, however, is that the download.asp page is giving me a "file not found" error, even though the file is correctly in the web directory "/_uploads/private/".
The file type could be one of several, including pdf, xls, docx, etc.
Is there something in my code that is not allowing the file to be found?
Thanks to user oracle certified professional, in the comments above.
What worked was adding "Server.MapPath" to resolve the file location.
Instead of using:
objStream.LoadFromFile(strFilePath & strFile)
I changed it to:
objStream.LoadFromFile(Server.MapPath(strFilePath & strFile))
Now the link triggers the file to properly download.

ASP.Net creating multiple sheets in excel with Response.Write

I usually use Response.Write to export my reports to an excel file.
E.g
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=MyFile.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
Response.Write("<table>");
Response.Write("<tr>");
Response.Write("<td>");
Response.Write("Hello");
Response.Write("</td>");
Response.Write("</tr>");
Response.Write("</table>");
Response.End();
Now i want to export an excel file with multiple sheets.
How can i accomplish this?
Tehnically speaking this is not export to excel, but you send a html with a wrong headers to trick browser to open this content with excel.
Better use NPOI (xls) or / and EPPlus (xlsx) and fully control your excel export and then you can create new sheets and more.
Update: In this SO answer you can see example of ashx handler that returns excel file that is created from DataTable :
C# create/modify/read .xlsx files

Resources