feat(报表导出): 增强分支机构报表导出功能
在Excel和PDF导出中增加省份分组显示,并添加统计信息 - 按省份分组显示分支机构数据 - 添加省份标题行和统计信息 - 优化表格样式和布局 - 在HTML模板中显示分支机构数量
This commit is contained in:
Binary file not shown.
@@ -25,7 +25,7 @@
|
||||
<tr class="{% if forloop.parentloop.counter|divisibleby:2 %}bg-gray-100{% else %}bg-white{% endif %}">
|
||||
{% if forloop.first %}
|
||||
<td class="px-6 py-4 whitespace-no-wrap text-left border-b border-gray-200" rowspan="{{ branches|length }}">
|
||||
{{ province }}
|
||||
{{ province }}({{ branches|length }})
|
||||
</td>
|
||||
{% endif %}
|
||||
<td class="px-6 py-4 whitespace-no-wrap text-left border-b border-gray-200">
|
||||
|
||||
@@ -290,17 +290,74 @@ def export_branches_xls(request):
|
||||
worksheet.title = "分支机构信息"
|
||||
|
||||
# 添加表头
|
||||
headers = ['分支机构名称', '信息系统类别']
|
||||
headers = ['所在省份', '分支机构名称', '信息系统类别']
|
||||
for col_num, header in enumerate(headers, 1):
|
||||
worksheet.cell(row=1, column=col_num, value=header)
|
||||
|
||||
# 获取所有分支机构数据
|
||||
branches = Branch.objects.all()
|
||||
# 获取所有分支机构数据并按省份分组
|
||||
branches = Branch.objects.all().order_by('location', 'name')
|
||||
from collections import defaultdict
|
||||
branches_by_province = defaultdict(list)
|
||||
for branch in branches:
|
||||
branches_by_province[branch.location].append(branch)
|
||||
branches_by_province = dict(sorted(branches_by_province.items()))
|
||||
|
||||
# 填充数据
|
||||
for row_num, branch in enumerate(branches, 2):
|
||||
worksheet.cell(row=row_num, column=1, value=branch.name)
|
||||
worksheet.cell(row=row_num, column=2, value=branch.category)
|
||||
row_num = 2
|
||||
for province, province_branches in branches_by_province.items():
|
||||
# 添加省份标题行
|
||||
worksheet.cell(row=row_num, column=1, value=f"{province}({len(province_branches)})")
|
||||
worksheet.cell(row=row_num, column=2, value="")
|
||||
worksheet.cell(row=row_num, column=3, value="")
|
||||
|
||||
# 设置省份标题行样式
|
||||
province_row = worksheet[row_num]
|
||||
for cell in province_row:
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = PatternFill(start_color="D3D3D3", end_color="D3D3D3", fill_type="solid")
|
||||
|
||||
row_num += 1
|
||||
|
||||
# 填充该省份的分支机构数据
|
||||
for branch in province_branches:
|
||||
worksheet.cell(row=row_num, column=1, value=branch.location)
|
||||
worksheet.cell(row=row_num, column=2, value=branch.name)
|
||||
worksheet.cell(row=row_num, column=3, value=branch.category)
|
||||
row_num += 1
|
||||
|
||||
row_num += 1 # 空行分隔
|
||||
|
||||
# 添加统计信息
|
||||
row_num += 1
|
||||
worksheet.cell(row=row_num, column=1, value="省份统计")
|
||||
worksheet.cell(row=row_num, column=2, value=f"总计 {len(branches)} 个分支机构,分布在 {len(branches_by_province)} 个省份")
|
||||
worksheet.cell(row=row_num, column=3, value="")
|
||||
|
||||
# 设置统计行样式
|
||||
stats_row = worksheet[row_num]
|
||||
for cell in stats_row:
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = PatternFill(start_color="FFD700", end_color="FFD700", fill_type="solid")
|
||||
|
||||
# 添加类别统计
|
||||
row_num += 1
|
||||
type_a_count = branches.filter(category='A型').count()
|
||||
type_b_count = branches.filter(category='B型').count()
|
||||
type_c_count = branches.filter(category='C型').count()
|
||||
worksheet.cell(row=row_num, column=1, value="类别统计")
|
||||
worksheet.cell(row=row_num, column=2, value=f"A型: {type_a_count}家 | B型: {type_b_count}家 | C型: {type_c_count}家")
|
||||
worksheet.cell(row=row_num, column=3, value="")
|
||||
|
||||
# 设置类别统计行样式
|
||||
category_row = worksheet[row_num]
|
||||
for cell in category_row:
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = PatternFill(start_color="FFD700", end_color="FFD700", fill_type="solid")
|
||||
|
||||
# 设置列宽
|
||||
worksheet.column_dimensions['A'].width = 25
|
||||
worksheet.column_dimensions['B'].width = 40
|
||||
worksheet.column_dimensions['C'].width = 15
|
||||
|
||||
# 设置响应
|
||||
response = HttpResponse(content_type='application/vnd.ms-excel')
|
||||
@@ -353,13 +410,28 @@ def export_branches_pdf(request):
|
||||
title = Paragraph("分支机构信息", title_style)
|
||||
elements.append(title)
|
||||
|
||||
# 获取所有分支机构数据
|
||||
branches = Branch.objects.all()
|
||||
# 获取所有分支机构数据并按省份分组
|
||||
branches = Branch.objects.all().order_by('location', 'name')
|
||||
from collections import defaultdict
|
||||
branches_by_province = defaultdict(list)
|
||||
for branch in branches:
|
||||
branches_by_province[branch.location].append(branch)
|
||||
branches_by_province = dict(sorted(branches_by_province.items()))
|
||||
|
||||
# 准备表格数据
|
||||
data = [['分支机构名称', '信息系统类别']] # 表头
|
||||
for branch in branches:
|
||||
data.append([branch.name, branch.category])
|
||||
data = [['所在省份', '分支机构名称', '信息系统类别']] # 表头
|
||||
|
||||
# 按省份填充数据
|
||||
for province, province_branches in branches_by_province.items():
|
||||
# 添加省份标题行
|
||||
data.append([f"{province}({len(province_branches)})", '', ''])
|
||||
|
||||
# 填充该省份的分支机构数据
|
||||
for branch in province_branches:
|
||||
data.append([branch.location, branch.name, branch.category])
|
||||
|
||||
# 添加空行分隔
|
||||
data.append(['', '', ''])
|
||||
|
||||
# 创建表格
|
||||
table = Table(data)
|
||||
@@ -368,19 +440,59 @@ def export_branches_pdf(request):
|
||||
style = TableStyle([
|
||||
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
|
||||
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
||||
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
||||
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
|
||||
('FONTNAME', (0, 0), (-1, 0), chinese_font), # 使用中文字体
|
||||
('FONTSIZE', (0, 0), (-1, 0), 14),
|
||||
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
||||
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
|
||||
('FONTNAME', (0, 1), (-1, -1), chinese_font), # 使用中文字体
|
||||
('GRID', (0, 0), (-1, -1), 1, colors.black)
|
||||
])
|
||||
|
||||
# 设置省份标题行的样式
|
||||
current_row = 1
|
||||
for province, province_branches in branches_by_province.items():
|
||||
# 省份标题行
|
||||
style.add('BACKGROUND', (0, current_row), (-1, current_row), colors.lightgrey)
|
||||
style.add('FONTNAME', (0, current_row), (-1, current_row), chinese_font)
|
||||
style.add('FONTSIZE', (0, current_row), (-1, current_row), 12)
|
||||
style.add('TEXTCOLOR', (0, current_row), (-1, current_row), colors.black)
|
||||
|
||||
current_row += 1
|
||||
|
||||
# 分支机构数据行
|
||||
for _ in province_branches:
|
||||
style.add('BACKGROUND', (0, current_row), (-1, current_row), colors.beige)
|
||||
current_row += 1
|
||||
|
||||
# 空行
|
||||
current_row += 1
|
||||
|
||||
table.setStyle(style)
|
||||
|
||||
# 添加表格到文档
|
||||
elements.append(table)
|
||||
|
||||
# 添加统计信息
|
||||
elements.append(Spacer(1, 20))
|
||||
|
||||
# 省份统计
|
||||
stats_style = ParagraphStyle(
|
||||
'Stats',
|
||||
parent=styles['Normal'],
|
||||
fontName=chinese_font,
|
||||
fontSize=12,
|
||||
spaceAfter=10
|
||||
)
|
||||
stats_text = f"省份统计:总计 {len(branches)} 个分支机构,分布在 {len(branches_by_province)} 个省份"
|
||||
elements.append(Paragraph(stats_text, stats_style))
|
||||
|
||||
# 类别统计
|
||||
type_a_count = branches.filter(category='A型').count()
|
||||
type_b_count = branches.filter(category='B型').count()
|
||||
type_c_count = branches.filter(category='C型').count()
|
||||
category_text = f"类别统计:A型: {type_a_count}家 | B型: {type_b_count}家 | C型: {type_c_count}家"
|
||||
elements.append(Paragraph(category_text, stats_style))
|
||||
|
||||
# 构建PDF
|
||||
doc.build(elements)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user