添加联系人Excel导出功能和合规管理人分类
This commit is contained in:
@@ -33,7 +33,8 @@ class Contact(models.Model):
|
||||
('机房/设备间巡检人', '机房/设备间巡检人'),
|
||||
('信息安全联系人', '信息安全联系人'),
|
||||
('兼岗', '兼岗'),
|
||||
('安全员', '安全员')
|
||||
('安全员', '安全员'),
|
||||
('合规管理人', '合规管理人')
|
||||
# 可以添加更多类别
|
||||
]
|
||||
# 修改为支持多选的 CharField
|
||||
|
||||
@@ -222,7 +222,13 @@
|
||||
</div>
|
||||
|
||||
<!-- 导出按钮 -->
|
||||
<div class="mt-6 flex justify-end">
|
||||
<div class="mt-6 flex justify-end gap-4">
|
||||
<a href="{% url 'export-contacts-xls' %}{% if request.GET.urlencode %}?{{ request.GET.urlencode }}{% endif %}" class="inline-flex items-center px-5 py-2.5 bg-gradient-to-r from-green-600 to-green-700 hover:from-green-700 hover:to-green-800 text-white font-medium rounded-lg shadow-md hover:shadow-lg transition-all duration-200">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
导出为Excel
|
||||
</a>
|
||||
<a href="{% url 'export-contacts-pdf' %}{% if request.GET.urlencode %}?{{ request.GET.urlencode }}{% endif %}" class="inline-flex items-center px-5 py-2.5 bg-gradient-to-r from-purple-600 to-purple-700 hover:from-purple-700 hover:to-purple-800 text-white font-medium rounded-lg shadow-md hover:shadow-lg transition-all duration-200">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
|
||||
@@ -15,7 +15,8 @@ urlpatterns = [
|
||||
path('branch/info/', views.Branchinfo, name='branchinfo'),
|
||||
path('statistics/', views.Statistics, name='statistics'),
|
||||
path('contact/', views.contact_list, name='contact-list'),
|
||||
path('contact/export/pdf/', views.export_contacts_pdf, name='export-contacts-pdf'),
|
||||
path('contact/export/pdf/', views.export_contacts_pdf, name='export-contacts-pdf'),
|
||||
path('contact/export/xls/', views.export_contacts_xls, name='export-contacts-xls'),
|
||||
path('equipment-images/', views.equipment_images, name='equipment-images'),
|
||||
path('public-screens/', views.public_screens, name='public-screens'),
|
||||
path('video-terminals/', views.video_terminal_list, name='video-terminals'),
|
||||
|
||||
@@ -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