首页 > 解决方案 > 将数据表导出到 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;                                         
                      }

标签: javascriptc#jquerydatatable

解决方案


推荐阅读