現在、以下のように画面側に表示された値を取得する処理を作成しています。
Excelファイル(report.xlsx)自体はダウンロードできるのですが、ダウンロードされたファイルを開くと
Excelでファイル'report.xlsx'を開くことができません。 ファイル形式またはファイル拡張子が正しくありません。 ファイルが破損しておらず、ファイル拡張子とファイル形式が一致していることを確認してください。
となりファイルが開けません。(0KBになっている。)
view
1 sheet.cell(row=2, column=3).value = d["name"]
の箇所を"Hello"などの固定値に変更しても同じエラーです。
利用しているExcel2016でセキュリティの設定を変更しても開けません。
同様のエラーがないか検索してみたのですが見つからず、原因が分かりません。
初歩的な質問で申し訳ないのですが、ご教授いただけないでしょうか。
view
1from django.views import generic 2from django.http import HttpResponse 3import openpyxl as px 4 5#... 6 7class DateDolwnload(LoginRequiredMixin, generic.ListView): 8 template_name = 'hoge/custom_detail.html' 9 model = Employee 10 11 def index(request): 12 13 return HttpResponse('hoge/custom_detail.html') 14 15 def post(self, request, *args, **kwargs) : 16 if request.method == "POST": 17 d = { 18 'name':request.GET.get('last_name_kana') 19 } 20 21 wb = px.Workbook() 22 sheet = wb.active 23 sheet.title = 'test' 24 25 sheet.cell(row=2, column=3).value = d["name"] 26 27 response = HttpResponse(content_type='application/vnd.ms-excel') 28 response['Content-Disposition'] = 'attachment; filename=%s' % 'report.xlsx' 29 30 31 return response 32
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/16 05:47