首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4数据输出到excel文件工作,但文件不可读

Rails 4数据输出到excel文件工作,但文件不可读
EN

Stack Overflow用户
提问于 2020-04-04 06:05:27
回答 2查看 229关注 0票数 0

由于某些原因,使用下面的代码生成的excel只在开发和暂存模式下工作。在生产服务器中,使用数据表生成的excel文件无法工作,并显示警告“如果在生产模式中生成某些内容,则无法打开xyz.xlsx”。我使用的是DataTables 1.10.18。

application.js

代码语言:javascript
复制
//= require datatables/datatables.min.js
//= require datatables/moment.js
//= require datatables/datetime-moment.js
//= require datatables/datatables_setup.js

HTML:

代码语言:javascript
复制
<table id="store_orders_table" class="table table-stripped">
              <thead>
                <tr>
                  <th class="bold-detail-text">Customer</th>
                  <th class="bold-detail-text">Order Items</th>
                  <th class="bold-detail-text">Order Date</th>
                  <th class="bold-detail-text">Payment Method</th> 
                </tr>
              </thead>
              <tbody>
                <% @orders && @orders.each_with_index do |order,index| %> 
                  <tr>
                    <td class="detail-text">
                      <%= order.name rescue "" %>
                    </td>
                    <td class="detail-text" style="min-width:102px;">
                      <%= order.order_item.name %>
                    </td>
                    <td class="detail-text" data-export="<%= order.payment.payment_date.strftime("%m-%d-%Y") %>"> 
                      <%= order.payment.payment_date.strftime("%B %d, %Y") rescue ""%>
                    </td>
                    <td class="detail-text">
                      <%= order.payment.payment_method.to_s.titleize rescue "" %>
                    </td> 
                    <td class="detail-text">
                      <%=order.comments %>
                    </td>
                  </tr>
                <% end %>
              </tbody>
              <tfoot>
              </tfoot>
            </table>

联署材料:

代码语言:javascript
复制
$('#store_orders_table').DataTable( {
    "language": {
      "emptyTable": "<i class='detail-text'>No records found</i>"
    },
    pageLength: 100,
    bFilter: true,
    dom: 'Brtipl',
    "order": [[ 2, "desc" ]],
    buttons: [
          {
            extend: 'excelHtml5',
            filename: 'store_orders_report',
            title: "",
            footer: false,
            header: true,
            text: '<i class="fa fa-file-excel-o"></i> Export as Excel',
            titleAttr: 'Export as EXCEL',
            classAttr: 'pull-right', 
            exportOptions: { 
              orthogonal: 'export', 
              columns: [0,1,2,3], 
              format: {
                  body: function ( data, row, column, node ) {
                      if( typeof $(node).data('export') !== 'undefined'){
                          data = $(node).data('export');
                      }                 
                      return data;
                  }              
                }
              }
          }
    ],
    columns: [
    { data: "0",render: function (data, type, row) { return data }},
    { data: "1",render: function (data, type, row) { return data }},
    { data: "2",render: function (data, type, row) { return data }},
    { data: "3",render: function (data, type, row) { return data }}, 
    ],
    "columnDefs": [
    {"targets": [ 0 ], "visible": true, "searchable": true },
    {"targets": [ 1 ], "visible": true, "searchable": true },
    {"targets": [ 2 ], "visible": true, "searchable": true },
    {"targets": [ 3 ], "visible": true, "searchable": true }, 
    ]
  }); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-05 06:00:11

在这里贴出答案,因为我在任何地方都没有找到它。在花了几个小时之后,它通过在production.rb中更改production.rb来工作。看起来,对ES5/ES6的支持对于预编译资产时的数据也是必要的。excel现在能够显示所有类型的字符,包括对其他语言字符的支持。以前,它没有打开excel,因为它有特殊的字符&一些日文内容。

代码语言:javascript
复制
  # Compress JavaScripts
  #config.assets.js_compressor = :uglifier
  config.assets.js_compressor = Uglifier.new(harmony: true)  
票数 0
EN

Stack Overflow用户

发布于 2020-08-20 13:46:32

您可以使用data.replace(/<\/?[^>]+(>|$)/g, "");清除html条带标记。

代码语言:javascript
复制
exportOptions: {
    format: {
        body: function(data, row, column, node) {
            return data.replace(/<\/?[^>]+(>|$)/g, "");
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61024385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档