ruby-on-rails - Rails 4数据表导出到excel文件正在工作,但文件不可读
问题描述
出于某种原因,使用以下代码生成的 excel 仅在开发和暂存模式下工作。在生产服务器中,使用数据表生成的 excel 文件不起作用并显示警告“Excel 无法打开 xyz.xlsx,因为某些内容不可读”,如果在生产模式下生成。我正在使用 DataTables 1.10.18。
应用程序.js
//= require datatables/datatables.min.js
//= require datatables/moment.js
//= require datatables/datetime-moment.js
//= require datatables/datatables_setup.js
HTML:
<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>
JS:
$('#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 },
]
});
解决方案
在这里发布答案是因为我没有在任何地方找到它的记录。花费数小时后,它通过更改 production.rb 中的 js_compressor 来工作。在预编译资产时,数据表似乎也需要对 ES5/ES6 的支持。excel 现在能够显示所有类型的字符,包括对其他语言字符的支持。以前由于特殊字符和一些日语内容而无法打开excel。
# Compress JavaScripts
#config.assets.js_compressor = :uglifier
config.assets.js_compressor = Uglifier.new(harmony: true)
推荐阅读
- python - 将附件添加到当前的电子邮件 Python 程序中
- swift - 具有不同单元注册的 DiffableCollectionViewDataSource
- c++ - 为什么 std::codecvt 仅由文件 I/O 流使用?
- sql - 用于选择具有多列值的多行的 SQL 查询
- parallel-processing - OpenMP 是否将多个原子指令视为单个关键部分?
- c - 逻辑或:不计算第二个操作数(6.5.14.4),但编译器生成警告:违反标准?
- android - AWS 移动开发工具包库会因为 Amplify 而消失吗?
- android - 在循环中改造入队太慢
- javascript - 将值推送到数组但作为对象?JS
- r - 如何用 r 中的向量替换数据框中的字段名称?