将模板信息导出到Excel中,然后在django中下载

我有一个显示一些信息的模板(每当我打开模板时,这个信息都会改变,因为显示来自不同账单的数据,所以它一直在改变)。这个数据在表格中是这样呈现的:

<table id="items"> <tr> <th class="tipo">Tipo de Factura</th> <th class="descripcion">Descripcion</th> <th>Precio</th> </tr> <tr class="item-row"> <td><div><textarea>{{fact.tipo_Factura}}</textarea></div></td> <td class="description"><textarea>{{fact.descripcion}}</textarea></td> <td><span class="price">$ {{fact.importe_sin_iva}}</span></td> </tr> </table> <table id="totales"> <tr> <td class="total-line">Subtotal</td> <td class="total-value"><div id="subtotal">$ {{fact.importe_sin_iva}}</div></td> </tr> <tr> <td class="total-line">Iva</td> <td class="total-value"><div id="total">$ {{iva}}</div></td> </tr> <tr> <td class="total-line">Precio Total</td> <td class="total-value"><textarea id="paid">$ {{total}}</textarea></td> </tr> </table> 

所以现在我必须在桌子底下放一个底部,当用户按下它时,信息必须被下载到一个Excel文件中并保存在用户计算机的某个地方。

有人有任何想法或可以指出任何(正确的)方向,看看如何做到这一点?

任何意见,将不胜感激。 谢谢

我在类似的地方有以下代码:

“放置底部” – 是链接,如下所示:

 <a href="/some/path/report"></a> 

urls.py

 ... url(r'^/some/path/report$', file_load_view), ... 

view.py

 from StringIO import StringIO from csv import DictWriter @require_http_methods(["GET"]) def file_load_view(self, request): f = StringIO() writer = DictWriter(f, ["Tipo de Factura", "Descripcion", "Precio", "Subtotal", "total", "paid"]) writer.writeheader() report_line = { "Tipo de Factura": fact.tipo_Factura, "Descripcion": fact.descripcion, ... } writer.writerow(report_line) report = f.getvalue() resp = HttpResponse(report, mimetype="application/octet-stream") resp["Content-Disposition"] = "attachment; filename='{}'".format("report.csv") return resp 

由于用户加载csv文件(与'exel'几乎相同)包含像这样的报告:

 Tipo de Factura,Descripcion,Precio,Subtotal,total,paid 1,2,3,4,5,6 

如果您的磁盘上的文件已经有report可能相当于:

 ... fd = open("report/path/report.csv") report = fd.read() ...