import datetime
from openpyxl import Workbook
excel = Workbook()
excel_writer = excel.active
nav_titles = [
"고객 주문번호",
"받는분성명",
"받는분전화번호",
"받는분기타연락처",
"받는분주소(전체, 분할)",
"배송메세지1",
"품목명",
"내품코드"
]
excel_writer.append(nav_titles)
# 엑셀 내용
user_data = ['1', '2', '3', '4', '5', '6', '7']
excel_writer.append(user_data)
# 엑셀 파일명 및 저장 경로
now = datetime.datetime.now()
now = now.strftime("%Y%m%d%H%m%s")
excel.save(f"/Users/khh180cm/Desktop/{now}.xlsx")
1차 수정
def download_excel(self, request, queryset):
# order_set = queryset.select_related('status', 'user', 'address').filter(status__id__range=(2, 3))
order_set = queryset.select_related('status', 'user', 'address').filter(status__id=3)
if not order_set:
msg = "[상품 준비 중]인 주문이 존재하지 않습니다."
response = HttpResponse(msg, status=400)
return response
now = datetime.datetime.now()
file_name = now.strftime("%Y%m%d%H%m%s") + ".xlsx"
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = f"attachment; filename={file_name}"
excel = Workbook()
excel_writer = excel.active
# excel nav title
nav_bar = [
"고객 주문번호",
"받는분성명",
"받는분전화번호",
"받는분기타연락처",
"받는분주소(전체, 분할)",
"배송메세지1",
"품목명",
"내품코드"
]
excel_writer.append(nav_bar)
# excel contents
for order in order_set:
# 주문번호
order_number = order.order_number
# 주문명
order_name = order.order_name
# 받는분 전화번호
phone_number = order.address.phone_number
# 수령인
recipient = order.address.recipient
# 주소
address = order.address.name
each_row = [order_number, recipient, phone_number, "", address, "", order_name, ""]
excel_writer.append(each_row)
excel.save(response)
return response
download_excel.short_description = "대한통운 양식 다운로드"