导出
# Excel 导出
class ExportView(View):
MyModels = AuthSoft
MyFILES = A_S_FILES_MAP
NAME = "Test"
def get(self, request):
try:
data = json.loads(request.GET.get("data", None))
except:
return HttpResponse("")
search_data = {}
if not data:
data = {}
for k, v in data.items():
search_data["{}__icontains".format(k)] = v
qs = self.MyModels.objects.filter(**search_data).order_by("-id")
response = self.write_excel(qs)
filename = "{}{}.xls".format(timezone.now().strftime("%Y-%m-%d"), self.NAME)
response["Content-Disposition"] = "attachment; filename={}".format(filename)
return response
def get_style(self, name, height, bold=False):
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style.alignment = alignment # 数据居中
return style
def write_excel(self, qs):
"""将数据写入 Excel中""