0

I create an excel file in ASP with:

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=excel.xls" 
Response.ContentType = "application/download"

It's possible to SAVE the xls file on server - to download next?

another question - When I open the xls file on excel I receive an error about the file format. why?

the code:

<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
<style>
<!--
@page
{ 
mso-header-data:"&CDate\: &D\000A<%=strHeader%>";
mso-footer-data:"&CPage &P";
mso-page-orientation:landscape;
margin:1in .3in .6in .4in; 
}
br
{
mso-data-placement:same-cell;
mso-width-source:auto;
}
.wborder {
border-color: #FFF; 
}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>EXCEL</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo/>
</x:Print>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number> 
</x:Pane>
</x:Panes>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml><![endif]--> 


 </head>
 <body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
 <table border="1">
 <tr>
  <td class="wborder" width="100"></td>
  </tr>
  </table>
  </body>
   </html>

tks!!
DANIEL
  • 515
  • 7
  • 20
  • Yes you can save it to a file, but then you don't use Reponse to save it as that is going to the browser. Exactly what error is it giving about the format? Have you saved the file and examined what it looks like? – Dijkgraaf Feb 03 '16 at 20:28
  • @Dijkgraaf hi! tks for the help! the error: the file format and extension of don't match. the file could be corrupted or unsafe - The file opens 100% ok. but my concerns is about the "unsafe" word.. I know - its not unsafe.. but client dont.. – DANIEL Feb 03 '16 at 20:42
  • Then check the format. It is probably actually a csv file rather than an excel file. For us to confirm that you would have to show us the code that creates the file. – Dijkgraaf Feb 03 '16 at 20:50
  • @Dijkgraaf Hi! I paste the code above. the final code has a secound row with data from db. – DANIEL Feb 03 '16 at 23:29
  • 1
    If what you've posted is supposed to be the Excel file, it isn't `.xls`; it would be `.xlsx`, which is an Excel XML file format. `.xls` is a binary format, and what you've posted isn't binary, which means that the error you're receiving is exactly right: Excel is expecting a binary .xls file, and you're not providing it with one. The proper content type is `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`, and the proper extension would be `.xlsx`. See http://stackoverflow.com/q/4212861/62576 – Ken White Feb 03 '16 at 23:30
  • Following on from Ken's comment, the simplest fix would be to tell the browser to expect a .xlsx as per http://stackoverflow.com/questions/2937465/what-is-correct-content-type-for-excel-files, Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and a .xlsx extension – Dijkgraaf Feb 03 '16 at 23:34
  • 1
    BTW, your *another question* should have been exactly that - a separate question asked in it's own post. The two are unrelated, and should have been asked separately. – Ken White Feb 03 '16 at 23:34
  • @KenWhite Hi! tks! I alredy have tried with .xlsx too (but with application/vnd.ms-excel) - and retry now with application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - but, doesnt work. Now Excel cannot open the file and nothing happens. (didn't recognize the format or the file is corrupt) – DANIEL Feb 03 '16 at 23:41
  • Just because you say the response type is Excel doesn't create an Excel file, just like saying your car is a Ferrari doesn't make your Ford become one. :-) How are you *actually creating the Excel file*? Setting the response type only works if what you're providing as a response is already that type of data; it doesn't magically convert something else into it. As you've said *I create an Excel file in ASP*, how (other than setting the content type of the response) are you creating the file? – Ken White Feb 03 '16 at 23:47
  • Hi @KenWhite - first off all, tks a lot for your help! - I never create an Excel file with Classic ASP without any component. (like ExcelWriter LE). This is my first experience. The code is almost like I post on question. the 3 Response on Header and HTML code with table. – DANIEL Feb 04 '16 at 00:00
  • Do you have Excel installed on the machine where you're at now? How about a good text editor like Notepad++ or a hex viewer? If you have those, create a new Excel file, put a couple of rows of numbers in it, and save it. Then open that .xlsx file with either your hex viewer or Notepad++ and look at what's there. Chances are you can't read it, because what you're returning as an Excel file isn't one. (With an .xlsx file, you can use 7Zip to open it (because it's actually a .zip file), and view the contents, which includes a couple of folders and some XML files. (continued) – Ken White Feb 04 '16 at 00:31
  • You're not *creating an Excel file* just by setting the response contenttype as you are. Read what I wrote about calling your Ford a Ferrari not making it a Ferrari, no matter how many times you say it's a Ferrari. It's still a Ford. You're stating that you're returning an Excel file but not returning an Excel file, which is why Excel won't open it. – Ken White Feb 04 '16 at 00:32
  • Hi @KenWhite I understand what you said. And I think you are right. But as I said - its my first experience - I never create an Excel file without any component. My host upgrade his server to W12 and all components are not installed. (no ExcelWriter, no OWC). Then, I follow this instructions from MS - https://support.microsoft.com/en-us/kb/271572 - Its WORKS! I can control everything - including page orientation.. BUT.. when I open the file an "alert message" appears - "file format and extension of don't match. the file could be corrupted or unsafe" - – DANIEL Feb 04 '16 at 03:01
  • Open the file? Yes / No.. If Yes - open - and its 100% ok. But the alert message is annoying - and the "unsafe" word isn't so cool.. I'm opening the file with EXCEL 2013 - and the MS instruction is for 2007. I think thats the real problem.. MS change something in 2010 and 2013 versions - and display the message for "faux" Excel files - as you said. My problem is - I cant find any tutorial on web about how I can create a "real" xls in CLassic ASP - only this old instruction from MS. But my real problem is to SAVE the file on server. tks a lot! – DANIEL Feb 04 '16 at 03:09

0 Answers0