如何在导出的xlsx文件中的表底部添加sum行?
我在他们的医生身上没有看到这样的东西,但也许你也做了类似的事情。我只想总结一下total_price
列。
我的资源如下:
class CERequestResource(resources.ModelResource):
related_component__service = Field(attribute='related_component__service')
related_product__title = Field(attribute='related_product__title'')
related_component__component_name = Field(attribute='related_component__component_name')
related_component__task_description = Field(attribute='related_component__task_description')
related_component__position = Field(attribute='related_component__position')
number_of_units = Field(attribute='number_of_units', column_name='# of Units')
related_component__margin = Field(attribute='related_component__margin')
total_price = Field(attribute="total_price")
model = CERequest
fields = ('id', 'related_component', 'related_product', 'number_of_units', 'total_price', 'margin')
这真的很容易——重写一个这样的方法:
def after_export(self, queryset, data, *args, **kwargs):
total = 0
for row in data.dict:
total += Decimal(row["total_price"])
# this list must equal the width of your row
# with the total appearing at the same position as
# 'total_price'
data.append(["", "", "", "", total, ""])
请注意,您的示例被声明为错误。fields
和model
需要在Meta
类声明下:
class Meta:
model = CERequest
fields = ('id', 'related_component', 'related_product', 'number_of_units', 'total_price', 'margin')