javascript - 将数据表导出到 Excel 无法打开文件
问题描述
我正在尝试将 DataTable 导出到 excel 文件 (.xlsx) 中。
在 jQuery 中,我删除了不需要的行。
当我尝试打开 excel 文件时,它显示:“Excel 在 '[filename].xlsx' 中发现了不可读的内容。您要恢复此工作簿的内容吗?如果您信任此工作簿的来源,请单击是。 "
错误显示为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog
xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error128600_02.xml</logFileName><summary>Errors were detected
in file '[filename].xlsx'</summary><removedRecords summary="Following is a
list of removed records:"><removedRecord>Removed Records: Cell information
from /xl/worksheets/sheet1.xml part</removedRecord></removedRecords>
</recoveryLog>
The exported file is opening fine in OpenOffice with removed rows.
这是删除不需要的行的代码:我的 DataTable 中有 5 列,并根据第 3、4、5 列删除不需要的行
customize: function (xlsx) {
var exportData=[];
var sheet =xlsx.xl.worksheets['sheet1.xml'];
var clR = $('row', sheet);
var clR = $('row', sheet);
$('row', sheet).filter(function () {
var attr = $(this).attr('r');
if(attr>3)
{
if($(this).context.children.length===5){
var Item1= parseInt($(this).context.children[2].children[0].textContent);
var Item2= parseInt($(this).context.children[3].children[0].textContent);
var Item3= parseInt($(this).context.children[4].children[0].textContent);
if(Item1===0 && Item2 ===0 && Item3===0 ){
return true;
}
else{
exportData.push([{ key: 'A', value: $(this).context.children[0].children[0].textContent },
{ key: 'B', value: $(this).context.children[1].children[0].textContent },
{ key: 'C', value: $(this).context.children[2].children[0].textContent },
{ key: 'D', value: $(this).context.children[3].children[0].textContent },
{ key: 'E', value: $(this).context.children[4].children[0].textContent }]);
}
return false;
}
}
}).remove();
//update Row
clR.each(function () {`enter code here`
var attr = $(this).attr('r');
var ind = parseInt(attr);
if(ind>3){
$(this).remove();
}
});
// Create row before data
$('row c ', sheet).each(function () {
var attr = $(this).attr('r');
var pre = attr.substring(0, 1);
if(pre>3){
$(this).remove();
}
});
function Addrow(index,data) {
msg='<row r="'+index+'">'
for(i=0;i<data.length;i++){
var key=data[i].key;
var value=data[i].value;
msg += '<c t="inlineStr" r="' + key + index + '">';
msg += '<is>';
msg += '<t>'+value+'</t>';
msg+= '</is>';
msg+='</c>';
}
msg += '</row>';
return msg;
}
//insert
var addrows="";
exportData.each(function (item,index) {
var r1 = Addrow(index+4, item);
addrows=r1+addrows;
});
sheet.childNodes[0].childNodes[1].innerHTML = sheet.childNodes[0].childNodes[1].innerHTML+addrows;
}
解决方案
推荐阅读
- numpy - 随机像素?(矩阵,Numpy)
- arrays - Powershell - 检查变量是否在数组中
- perl - Perl 的 `curses` shim 中是否有一个(或两个)可靠的错误?
- python - Python 诗歌 - 如何安装可选依赖项?
- javascript - 用于 javascript 的 VS Code Intellisense 不提供建议?
- wordpress - 当 /var/www/html 已经安装在主机中时,如何使用带有 wp-cli 容器的 wordpress 容器?
- javascript - 如何在 TypeScript 中正确键入“省略”函数以避免错误消息?
- javascript - 如何使用 unirest setState 做出反应
- android - 远程设备上的 Google Chrome 检查器无法正常工作
- php - php从网页解析字符串