장고 엑셀로 데이터 받기

Pywiki
Sam (토론 | 기여)님의 2022년 8월 29일 (월) 23:14 판 (→‎뷰 작성)
둘러보기로 가기 검색하러 가기

1 개요

장고로 데이터를 가공하여 엑셀로 다운받는 기능을 구현하기 위한 기능을 설명하기 위한 문서이다.

엑셀 데이터 생성에 대해선 분류:엑셀 헨들링 문서를 참고하자.

2 뷰 작성

from django.http import HttpResponse
import 취향에 맞는 엑셀 헨들러

def excel_download(request):
    response = HttpResponse(content_type="application/vnd.ms-excel")
    response["Content-Disposition"] = 'attachment; filename=' + '파일명' + '.xls'
    
    # 알아서 데이터 입력.
    wb = xlwt.Workbook(encoding='utf-8')  # 인코딩
    ws = wb.add_sheet('시트이름')
    #.... 알아서 데이터 입력
    
    wb.save(response)  # response에 저장.
    return response