在django中下载Excel文件

时间:2017-09-15 08:36:12

标签: django python-2.7 pandas django-rest-framework

使用Postman测试一个应该允许用户下载excel文件的视图。这是视图..

@api_view(['POST'])
@permission_classes([AllowAny])
@parser_classes((MultiPartParser, ))
def claims_report(request):
    in_count = 30
    # in_count = claims.filter(_type='inpatient').count()
    out_count = 40
    # out_count = claims.filter(_type='outpatient').count()
    in_total_claim_amount = 3000000
    # in_total_claim_amount = reduce((lambda x, y: x+y), claims.filter(_type='inpatient', ).values_list('_type', flat=True))
    out_total_claim_amount = 50000000
    report_data = {}
    report_data['inpatient_count'] = in_count
    report_data['outpatient_count'] = out_count
    report_data['inpatient_amount'] = in_total_claim_amount
    report_data['outpatient_amount'] = out_total_claim_amount

    _file = BytesIO()

    df = pd.DataFrame([report_data])

    writer = pd.ExcelWriter(_file, engine='xlsxwriter')
    df.to_excel(writer)
    writer.save()

    _file.seek(0)
    workbook = _file.getvalue()

    response = StreamingHttpResponse(workbook, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % 'test_report'
    return response

当我测试这个视图时,我无法从邮递员那里得到下载提示。相反,我从Postman的响应中得到了损坏的数据。这是一个示例输出:

PK
<?/K�^��txl/worksheets/sheet1.xml�RMo�0��WD���2�   �E0@�aҴ�{h�6���$���/i尞l���g7���*����":pJ@%�J�G��su�H��B��D�C��Mؠޘ�'�LDk�  c&)�f�5(�d�+a]�sfj
"m����߳JHE;����f�L`�ɶe;
��ξ)dmh��a~�!��l8Y��a;�[BcNbb��JH,�nJ�bkč_\��V��]���4I!�Ҿc�2/��-�q��!�7��N��g�s_m1����b����O}���3�sƲ���;������+��A'�������ە��Zϲ���?vr�Z��*t.�!%dn'>x�Dw�oc�u�)Y��X�½A�>Q�!�C���U�PK
<?/K�j%H&xl/workbook.xml�Q�N�0����ij#Z5��KTB�Di�&�4V;���=�T)p�3����z�:6�}�u���Iui���>6��7���Zpe4�pB��j�{�4��h^�j��E��Ɔ��iQ�Kel�=Q��\k�W#�FEigQå�������T�,�ޔ]�ڟM,*�i{W��A�����9�m��Z���)����ES���?����N�@f�����2����j�;�+��i:C�Vb�~�eǝ���9�S��id�X?���&!���   ��9̳,��/��~cez�pB���'l��]�dp�J�JJ�ИNg�X�)uGګ~6|0Cc��PK
<?/K�ͮr�xl/sharedStrings.xml}�A
�0н����""����ZB;�@3�����M�q�y�?����S8$��*JPHuj�,�O�����w���*�0̢r��B+�����H=R�k�����~@�p�(���������Hbaj�p����ppF\��K@����-����^��4�����l@��PK
<?/K^���wdocProps/app.xml���N�0E�|E�=uR!�T9F��X�D�Xg�X8������IՐ+��3su}2��ڵ6� ��d�,g8�+�%{�ܝ�c�r���A����J��U�"�,%8,YC��n�U8Kc�&����T�-�um4�x�ނ#>��K;WAu�@vH\t������7�����(���ot��k�nw��ӡHAk����^�OK����2�ZY���T���2��hс&34iks��*��d��F9bۡ�
HQ����
��cs�S�T�Y�$N�|I�qc�>�+��bJ
<0� ��+~�O����mP.-�����7|
���
    <m�u�"T��u�
q������F�-TG��A�χ.��,O�p�Ǟ�_oY~PK
        <?/K�v&PdocProps/core.xml���j�0E��
��-;!IklڒU���t'��#j=��:��ʏ8   xU�F�w���mO�~�X�d��(FH��U���]x��d�VrԂE��.�:����Q��`�6�:GG�t���G�F�!�xPF篦�oR^��p�Gp�DD#��  �L��P��,N�_�����r�ܵf�gqr�,��M�DͲ��� �ܿ����\v�����є N�"�����X��+>p`���g��A�:`��q�����ܡb'�0~�Uoҕ?믮�M�(�&�&�C��OP�PK
        <?/Ka]I:O�[Content_Types].xml���n�0E�����*1tQU�E���\{B,������P[Q��M�d��sǎ<-�- �
���'2�:맥x�<�w"CR�(<�b(Fë�d3n�X��(�K���Fa"x�T!5��5MeTz�� oz�[��'�S�!��G���Q�����  ��a-lY�P1:�q].��E�7��;;
�6�5��Kh+6}��3���*ыjX%M��"J��]��Ue5�Ǽ��@L�Y�e>��!����=j�O$.�DZ9��GŘ@��q��������6��9�i����ök�(�O�wb��r��?����y��7J|
\��{os��>~PK
        <?/Ki���=
xl/styles.xml�Tߋ�0~�_�~�*�h�z�
��n}�5�����_�I�w,�C_���of��䏓��̭F4�?P�um����9�>Q��I�yAg��c�!w0K��s�+h0|IW�\1wo�q�5V1@�v�,g��IJ&���(&4-��hp�6���fK���93�m�4)��Hc     =�"�)ߘ�>�2%�Ù����XLb�����Q+,�����b����>�C�4X^��&�n�;��4��K֭�m���c��%o��z���   `�`��Lz�K�>���PP��aF��f�.��&6�B7���tya�ϲ(Qͥ|�L�U�����Q|o
���?ɋ��.f����߳E�m�.Z2�+�[����M�0��h�|�{
���*E��H�..�/���x�n�A@�>�����E�u� ŕ�k�������c!wmV�� �+�"g3mj�]`�IWU���-%��͂n�OވQ}^Q��������N�����+�PK
        <?/K�FT�Rxl/theme/theme1.xml�YM��D��+F���;ͮ��6٤����ݴ�lj=��{���nsC�       Q$nP����_�PE�_��G��f�Ͷ��9$���~����1CGDHʓ��\�Y�$>h��;�������I�OHۚi]���*�T�  �Dn�)�nڶ�a��<%   �qcKځ���6fv�Vk�1����ۣ�  d,�����D�l�g���%�96;ُ��.���r~< ���
n��Z��쭫�����]?��t%A0��t"�    ���qegο^�_��z�nϙ�����Rg    ��[Ng�S�˼�5��V���~���x|c�w��Z�ݮW���-����v����7���+M���A��x    ��s�9d��
#���,([ˮ�>Q�r-���� .V4Aj������PP�    ��kw�-_.me���MU��(�Pȫ�?�z��z���᳓�?�
        <zt��'�
��:���>���OПO�}��K3^���~���_�0�|�Փߟ=y��g|���x��4&�"���`�A��Q"L+8��SQxk��    �!U���L����]#1Q�܍�
p�s���h�n&K7g��f�b��0>2���
mo�B&S�nD*j�3�6IB���1!�{�V��G}�%)t���F��P��n��25)���f�.�pfb�C��H(�L,  ���:�(5�1ӑ7��LJN�_q�T�0�z��Ds[L+��b�Dư�i\E
E�&�M̹����n��Ԩ3M"��C�b�ϕQ ^��l
q���pߥD�����02'Hvg"ʮ]�1M�jƌB7~ߌg�mx4�J�t^��6�
            <I� ������ヒ}wU-��m
�����_�rHQ�Ք��2o�����/r��L�FpY���B��k$�����0�)�qr   �,Y��\�I�Z�;?NR0>��fg@@c�ǃb���
�l�U(uA�����W�N�Sה�xfiޙ�l͛P
g�Y/DC�`F����YX.
                <D2�)c�
qk���z�i�6o'm� ������Rm)J�r9���BǠ�W�,���m�`���8~2k@��I��Ui�k������tj+
��H�T;XFU~k��$Y�_���c����E����ZاCKF#��;�ey�O�Qp��l"0���P  ό�l!�B�2���_V��W4eu`�F��I--�<��됯4�����)�4�{wM�2��F��`e9ڶ�P�.�F��rY����TB,{ߜ�J�}��Q4�0R4D�B�S� d_�v���Sן�3Fe���+��wH�d����P4�&�#r��٦������㮘|�����"����G��۩p�Gm�lq�[�Q���e_и���b���>�O��R�,���tni�e���1j�֊x_���9����g�{sg{_{g��^.Q[;�䫥?���>�ށ�҄)Y�MzG���/�c/H��PK
                    <?/K��I��K_rels/.rels���N�0@�|E���nH��� ��&4>�$n���ă�@�i�q��g���<���R�,��·����iq*�#2p���f�L#J�ɽ�YH�z���u�=M�+��O�iB)����v������t�ɀ愩��@ں%��1�5ln[o���a� gZ��(dL��y�W��*P��]V׻�=��HС���hS�N��Z�u�]   �όKB������#wY  c�2�'7�|PK
                    <?/KDu[���xl/_rels/workbook.xml.rels���j�0D��
��ZvJ)�s)�\�����LlIh�i�� M��Čؙ����Ћ&�WP%�&�η
>wo�� �����H�����k�3�H"�xR��㋔d�����&�As���Q��nQ.��I�i�W�bk���@�ƈ��M�|
�k@�7*�wH{r��Cuj�\,���*r*��0�9a8���I�ͻ�9��>/�q���W��;��~p��6��ڿ0����#PK
                    <?/K�^��t��xl/worksheets/sheet1.xmlPK
                    <?/K�j%H&���xl/workbook.xmlPK
                    <?/K�ͮr���xl/sharedStrings.xmlPK
                    <?/K^���w���docProps/app.xmlPK
                    <?/K�v&P���docProps/core.xmlPK
                    <?/Ka]I:O����[Content_Types].xmlPK
                    <?/Ki���=
��uxl/styles.xmlPK
                    <?/K�FT�R���
xl/theme/theme1.xmlPK
                    <?/K��I��K��|_rels/.relsPK
                    <?/KDu[������xl/_rels/workbook.xml.relsPK

��

为什么我不能下载文件?我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

这对我来说很好

if os.path.exists(file_path):
    with open(file_path, 'rb') as fh:
        response = HttpResponse(fh.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        response['Content-Disposition'] = 'attachment; filename=mywebsitename.xlsx'
        return response
else:
    raise Http404

答案 1 :(得分:0)

示例:

response = HttpResponse(fh.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = 'inline; filename=%s.xlsx' % 'test_report'
return response