0

I'm developing web application on python. But the client wants a tool that exports current page to excel. So I had to write an extra code in every pages (views) that converts the table to excel using xlwt (python module).

But I have a better solution. It sends a current page's content html to the server and the server would respond this html table as excel type. Like this:

response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-8'
Response.ContentType = "application/vnd.ms-excel" 

This works quite good. But I'm curious about whether every browser supports this excel respond!?!?!

If not it would be a big problem to me :(

Or do browsers support it if only MS Excel or OpenOffice is installed on the client computer?

Any other "global" solution or ideas?

jargalan
  • 5,006
  • 5
  • 27
  • 36

2 Answers2

2

Setting the content type like this is notification to the client that the document has a specific type -- in a sense, you're lying to the browser by telling it you're sending it an Excel spreadsheet when you're actually sending it an HTML document. The effect of this is that the browser will try to open it as if it was an Excel spreadsheet, and as Excel knows how to import HTML, it 'works' -- as you've discovered.

If the client doesn't have Excel, or other software configured to open Excel documents, the browser will probably offer to save the document. It won't magically display it as a spreadsheet anyway. I'm not sure what else you'd expect?

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
1

Properly converting it into an actual Excel file would definitely be the best way to go.

That said, if

  • You give the file name the .XLS extension
  • Send along the proper MIME type
  • Excel or some other program registered to handle XLS files is installed on the user's computer

the "fake HTML as Excel" method should work fine. I have never tried opening a HTML file masked as XML in OpenOffice, though - you should definitely try that out and see what it does with it.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • yes I gave the xls file extension, and I'm sure that the mime sends properly. So the problem is that excel or related app must be installed on user's comp? Ah.. the I need convert it on server perhapse >.< duh – jargalan Jun 15 '11 at 08:58
  • btw I work on Ubuntu using OpenOffice and tested it on both Chrome v12 and FF v3.6. it works just what i expected. But I think the users would download it as excel only they have apps that handle it mostly, arent they? – jargalan Jun 15 '11 at 09:07
  • 1
    @Zomboid yes, but it could be that other applications that can handle *real* Excel files can't handle HTML files masked as such. I wouldn't count on it. – Pekka Jun 15 '11 at 10:06