Note that there are some explanatory texts on larger screens.

plurals
  1. POCherrypy: Generate excel file dynamically
    text
    copied!<p>I am trying to have the functionality of allowing users to save table data into Excel file. I am trying to use an old trick of saving the html table code into the file and naming the file with an excel extension, which should show the table in excel.</p> <p>I have the following code for showing submit button:</p> <pre><code>output = '&lt;form name="export_excel" action="/excel" method="POST"&gt;' output += '&lt;textarea id="in" rows="1" cols="1" name="tableData" class="excelText" value="%s"&gt;' % tableData.strip().replace('&lt;','excellt').replace('&gt;', 'excelgt').replace('"','exceldblqt').replace('\'','excelsglqt') output += '&lt;/textarea&gt;&lt;input type="submit" value="Export table to Excel" title="Click here to save the table data in Excel file"&gt;&lt;/form&gt;' </code></pre> <p>Here I am putting the table data into the textarea, which is set to be hidden by a stylesheet. I munged the tabular data put into the textarea so that html tags in tableData do not corrupt current page view.</p> <p>The called function: excel(tableData) is like this:</p> <pre><code>@cherrypy.expose def excel(self, tableData): cherrypy.response.headers['Content-Type'] = "application/vnd.ms-excel" return tableData.replace('excellt','&lt;').replace('excelgt','&gt;').replace('exceldblqt','"').replace('excelsglqt','\'') </code></pre> <p>This does the trick of setting context type which prompts user to open/save the excel file. But seems the returned values are not getting into the file. The file is always empty.</p> <p>I assumed anything returned should be streamed to the user and get into the file as content. Any ideas? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload