Cherrypy: Generate excel file dynamically
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.
I have the following code for showing submit button:
output = '<form name="export_excel" action="/excel" method="POST">'
output += '<textarea id="in" rows="1" cols="1" name="tableData" class="exce开发者_如何学GolText" value="%s">' % tableData.strip().replace('<','excellt').replace('>', 'excelgt').replace('"','exceldblqt').replace('\'','excelsglqt')
output += '</textarea><input type="submit" value="Export table to Excel" title="Click here to save the table data in Excel file"></form>'
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.
The called function: excel(tableData) is like this:
@cherrypy.expose
def excel(self, tableData):
cherrypy.response.headers['Content-Type'] = "application/vnd.ms-excel"
return tableData.replace('excellt','<').replace('excelgt','>').replace('exceldblqt','"').replace('excelsglqt','\'')
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.
I assumed anything returned should be streamed to the user and get into the file as content. Any ideas?
精彩评论