我已经在我的项目中使用带有ruby 2.6.3的rails 5.2.0中的caxlsx(v3.0.4(实现了导出功能。我已经实现了导出功能并使用了两次。一方面工作正常,但另一方面它导致了一些垃圾重定向,而不是下载导出的excel文件。以下是我的代码示例:a.视图:
link_to export_customers_buyers_path
b。控制器内
def export_customers
@customers = @client.customers
package = Axlsx::Package.new
workbook = package.workbook
field = []
field_new = []
@client.custom_field_hstore.each do |field_name, _is_reuire|
field << "#{field_name}"
field_new << "#{field_name}"
end
workbook.add_worksheet(name: 'Buyers List') do |sheet|
sheet.add_row(['S.No', field[0], field[1], field[2], field[3], field[4], field[5], field[6], field[7], field[8], field[9], field[10],
field[11], field[12], field[13], field[14]])
counter = 0
@customers.each do |customer|
counter += 1
sheet.add_row([counter, customer.customer_detail_hstore[field_new[0]], customer.customer_detail_hstore[field_new[1]],
customer.customer_detail_hstore[field_new[2]], customer.customer_detail_hstore[field_new[3]],
customer.customer_detail_hstore[field_new[4]], customer.customer_detail_hstore[field_new[5]],
customer.customer_detail_hstore[field_new[6]], customer.customer_detail_hstore[field_new[7]],
customer.customer_detail_hstore[field_new[8]], customer.customer_detail_hstore[field_new[9]],
customer.customer_detail_hstore[field_new[10]], customer.customer_detail_hstore[field_new[11]],
customer.customer_detail_hstore[field_new[12]], customer.customer_detail_hstore[field_new[13]],
customer.customer_detail_hstore[field_new[14]]])
end
end
package.serialize("#{Rails.root}/tmp/basic.xlsx")
send_file("#{Rails.root}/tmp/basic.xlsx", filename: 'Buyers.xlsx', type: 'application/vnd.ms-excel')
end
c。它重定向的不是下载excel文件,而是向我显示一些垃圾值,就像它试图显示excel文件一样。如下所示:PK(!����� �[Content_Types].xml���n�0E��ZXJ:Ea;抄送�!�U�cz��K�M�,q�LE�s��Z��Hh��?/X^m��f���js���z�Y�s|UN";���@���;ؙ�w���Ş(Sz�"3立方英尺�z�d4k��t�:�u����&wh[��{G��]FBB��K\�五、���G�3{+0-�1.���dg������+�ݭ��ʝ4~����b���r��[5?PK(!�ꍎh�9_rels/.rels���J1�_����Dd�{ao"��63Sfڔ4���五、��.x���_B��%���J�M�#�ࡻ]]�~����J�(c�EUI*F�|�uq#F[ʘj�'�VjɃ��Mv@�i�K�_=��Su�ˬ_��G���f������o�=ELr"����7��}�œ�c�JG��9���(�[����%
���ds����"�<�SD�D��voPK(!����'��docProps/app.xml��� �0E�doSDJ�.��P�K���{!y���F�/�r8��½Z!eO�ŮiEh�y�����(z�.�"����Ǭ��;)�] ��)�(���L��我�Nd��m6t������[�_�#��˷���(��5/PK(!��&��docProps/core.xmlm�Qk�ǿJ�=1.�])b҇�>m0X����\SY4��ɾ��m��x���Şc�屈� ���.�r��}#u[��z�nѮb�P�[x���%�$�iG�)��{C1v���,:����Ci���-�"�7X�� �O���FtQ6bV��EA#0t�@{�如果�?���5pw�.hV��pLfrtr��aȆU����<�ů�RεT�FPa���V|�����.��$AO�k_����S�GU�$%E�oj��Ŗ>��&��|<����nV�/�十、�ު�PK(!�(��xl/rels/workbook.xml.rels��� �0�_���e����v+�J��֖���VQP�«�|�OV�K߉yn��0NRd�-SI8��6.�՞:�cOK�CpKD�5.�����Ii}�Bl}�N�似曾相识��4.��e�;S��W$��o�&��2N";D>8.�呃��ؓ���� >��_�p:�'xP���M�o^PK(!��'���xl/styles.xml�T]�� �+��q�.t��Rڥ���Wg���A͒����ib��m��sΓ^oR��(��Ş���!�gB����wv���i�nďN��G�V�TUD���6�nu��j'>m��aZUe�/�/��I�aD�*�� Pߟ�>{��dM��Қ� �3���ß�����J�Nr=�eE"@�0-�wP�p�4��Cδ�Ak���P��F��h��7�7�z���:-P�źN�CMB��Q�(!JW�p�ӕ]���ۅᶭғ��M�[�$��:��^Pcy!X}�$��ӭ4�*�z�.?���g�2'��쳕F�l!�t �v��SZ������1�����š ��M>�jy��E�6�f�����S+��P�.���*�I�E��潁U����|��}'$�Tx� �����I�X?�V���H�)�H�����PK(!��:>�4xl/workbook.xml��Ak�0����}uZJ�B�B)��;l?����4���ݿ�HWv�E!����-��Y"e�EsG!擃����3l��J|�$:���
(e���n��eAf����]�deb�AĒF����M>f�j���>v���0�;�q�E��'��dol�/�|��z�m��~����&��:�]���y�R�p��ǰV�����pjPK(!�=Xbd�Y�����h�i4;�f��f9U�V4'���ͭ�N��_;�gz�T������K��%�\�(/y�%O�%��(��bu����ȴ��我?گ�y@�_�+��/��ZΓT=߯D�vS�]%{�@ȴ�%}ce�"�~�^���=�NxS���^��z[��^P��五、��.�^o�b�G�f�五、���t�"�C����!"DƈL�"2&:�Wxh<��Aa�m��o���n������1.�|��t�U=^2�Bn�}��79�H�"}D�!2Fd����u�)�����一�9e�q�^!�[w�����一�l�"]Dz��2Dd��� "SDf�5.���[��Y-�:wܺd�M9�pjO�我�[� ��"�C����!"DƈL�"2秒�余;XUC�pά��,�����;��K��֗� � ��'��lC��H�>quot;D���#2Ad�Ȭv�6.�`���Q�ͽYv&�W��6.�\�춸�g8�'˶m�a>Y�{f���C��H�>quot;D���#2Ad���E��G"U�^���AڜF#N��%hb�"9�չAz�!�F_h$�ȷ�>�G��F�E�l��一���.��X3����Q��:���j/��g8?[�<�[+�e�o7�ݘ���yG/N-6q��.v��t�|܇nq6��PK4(!����� ���[Content_Types].xmlPK4(!�ꍎh�9��Q_rels/.relsPK4(!����'����UdocProps/app.xmlPK4(!��&����docProps/core.xmlPK4(!�(����Ixl/rels/workbook.xml.relsPK4(!��'��� ��9xl/styles.xmlPK4(!��:>�4.��)xl/workbook.xmlPK4(!�=Xb
注意:当我刷新这个垃圾页时,它会下载文件,但会停留在同一个垃圾页上
link_to export_customers_buyers_path, target: '_self'
解决了我的问题