Cherrypy:动态生成excel文件
我正在尝试提供允许用户将表数据保存到 Excel 文件中的功能。我正在尝试使用一个老技巧,将 html 表格代码保存到文件中,并使用 excel 扩展名命名该文件,这应该在 excel 中显示该表格。
我有以下用于显示提交按钮的代码:
output = '<form name="export_excel" action="/excel" method="POST">'
output += '<textarea id="in" rows="1" cols="1" name="tableData" class="excelText" 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>'
这里我将表数据放入文本区域,该文本区域设置为由样式表隐藏。我修改了放入文本区域的表格数据,以便 tableData 中的 html 标签不会破坏当前页面视图。
被调用的函数:excel(tableData) 是这样的:
@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','\'')
这实现了设置上下文类型的技巧,提示用户打开/保存 Excel 文件。但似乎返回的值没有进入文件。文件始终为空。
我假设返回的任何内容都应该流式传输给用户并作为内容进入文件。有什么想法吗?
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="excelText" 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论