Python 将模板信息导出到Excel中,然后在django中下载
我有一个显示一些信息的模板(每次打开模板时,该信息都会更改,因为它显示来自不同账单的数据,所以它会一直更改),该数据在表格中呈现为如下所示:Python 将模板信息导出到Excel中,然后在django中下载,python,django,excel,django-templates,Python,Django,Excel,Django Templates,我有一个显示一些信息的模板(每次打开模板时,该信息都会更改,因为它显示来自不同账单的数据,所以它会一直更改),该数据在表格中呈现为如下所示: <table id="items"> <tr> <th class="tipo">Tipo de Factura</th> <th class="descripcion">Descripcion</th> <th>Pr
<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>
蒂波德法图拉酒店
描述
普里西奥
{{fact.tipo_Factura}}
{{fact.description}}
${{fact.importe_sin_iva}
小计
${{fact.importe_sin_iva}
伊娃
${{iva}
精确总计
${{total}
因此,现在我必须在表格下方放置一个底部,当用户按下它时,信息必须下载到Excel文件中,并保存在用户计算机的某个位置
是否有人有任何想法或可以指向任何(正确的)方向,看看如何做到这一点
如有任何建议,将不胜感激。谢谢我在类似的地方有以下代码: “放置底部”-是链接,如下所示:
<a href="/some/path/report"></a>
Tipo de Factura,Descripcion,Precio,Subtotal,total,paid
1,2,3,4,5,6
在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”相同)包含如下报告:
<a href="/some/path/report"></a>
Tipo de Factura,Descripcion,Precio,Subtotal,total,paid
1,2,3,4,5,6
如果您的磁盘上已有“报告在文件中”report
可能等于:
...
fd = open("report/path/report.csv")
report = fd.read()
...
您好,那么我应该在报告中添加什么?对不起,我的英语不是那么好good@Nacho我添加了如何构建
报告的示例这是一个惊人的答案。真正地还有一个问题:如何解析结果。现在,我把所有的东西都写在两行里,简直无法读懂(我不知道这是不是一个词…)。非常感谢您的帮助。您可以使用Microsoft Excel打开report.csv
,这将使文件更具可读性。您也可以尝试使用和使用mimetype=“application/vnd.ms excel”
创建文件,但我不尝试这样做。