添加联系人Excel导出功能和合规管理人分类
This commit is contained in:
@@ -594,6 +594,73 @@ def contact_list(request):
|
||||
return render(request, 'contact_list.html', context)
|
||||
|
||||
|
||||
def export_contacts_xls(request):
|
||||
# 获取筛选参数
|
||||
branches_param = request.GET.get('branches')
|
||||
category = request.GET.get('category')
|
||||
contact_name = request.GET.get('contact_name')
|
||||
|
||||
# 构建查询条件
|
||||
filters = Q()
|
||||
|
||||
# 分支机构筛选(支持多选)
|
||||
if branches_param:
|
||||
branch_ids = [bid.strip() for bid in branches_param.split(',') if bid.strip().isdigit()]
|
||||
if branch_ids:
|
||||
filters &= Q(branch_id__in=branch_ids)
|
||||
|
||||
# 联系人类别筛选
|
||||
if category:
|
||||
filters &= Q(category__contains=category)
|
||||
|
||||
# 联系人姓名筛选
|
||||
if contact_name:
|
||||
filters &= Q(name__icontains=contact_name)
|
||||
|
||||
# 获取筛选后的联系人数据
|
||||
contacts = Contact.objects.filter(filters).order_by('branch__location', 'branch__name', 'name')
|
||||
|
||||
# 创建一个工作簿
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Font, PatternFill
|
||||
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
worksheet.title = "联系人信息"
|
||||
|
||||
# 添加表头
|
||||
headers = ['分支机构', '联系人类别', '姓名', '电话', '邮箱']
|
||||
for col_num, header in enumerate(headers, 1):
|
||||
cell = worksheet.cell(row=1, column=col_num, value=header)
|
||||
# 设置表头样式
|
||||
cell.font = Font(bold=True)
|
||||
cell.fill = PatternFill(start_color="4A90A4", end_color="4A90A4", fill_type="solid")
|
||||
|
||||
# 填充数据
|
||||
row_num = 2
|
||||
for contact in contacts:
|
||||
worksheet.cell(row=row_num, column=1, value=contact.branch.name)
|
||||
worksheet.cell(row=row_num, column=2, value=contact.category if contact.category else '')
|
||||
worksheet.cell(row=row_num, column=3, value=contact.name if contact.name else '')
|
||||
worksheet.cell(row=row_num, column=4, value=contact.phone if contact.phone else '')
|
||||
worksheet.cell(row=row_num, column=5, value=contact.email if contact.email else '')
|
||||
row_num += 1
|
||||
|
||||
# 设置列宽
|
||||
worksheet.column_dimensions['A'].width = 30
|
||||
worksheet.column_dimensions['B'].width = 20
|
||||
worksheet.column_dimensions['C'].width = 12
|
||||
worksheet.column_dimensions['D'].width = 15
|
||||
worksheet.column_dimensions['E'].width = 30
|
||||
|
||||
# 创建响应对象
|
||||
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
response['Content-Disposition'] = 'attachment; filename="contacts_info.xlsx"'
|
||||
workbook.save(response)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def export_contacts_pdf(request):
|
||||
# 创建响应对象
|
||||
response = HttpResponse(content_type='application/pdf')
|
||||
|
||||
Reference in New Issue
Block a user