axios.get('/download_excel', { responseType: 'blob' })
.then(response => {
// 응답으로 받은 파일 데이터 처리
const url = window.URL.createObjectURL(new Blob([response.data]));
// 다운로드 링크 생성 및 클릭
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
link.remove();
})
.catch(error => {
console.error(error);
});
from flask import Flask, request, send_file
import pandas as pd
app = Flask(__name__)
# 엑셀 다운로드 엔드포인트
@app.route('/download_excel', methods=['GET'])
def download_excel():
# 데이터베이스에서 데이터 조회 (예시로 pandas DataFrame 사용)
data = pd.DataFrame([
{'Name': 'John', 'Age': 25},
{'Name': 'Alice', 'Age': 30},
{'Name': 'Bob', 'Age': 35}
])
# 데이터프레임을 엑셀 파일로 변환
excel_data = data.to_excel(index=False)
# 엑셀 파일 전송
return send_file(excel_data, attachment_filename='data.xlsx', as_attachment=True)
if __name__ == '__main__':
app.run()
댓글