【问题标题】:Enable header filters in excel export using jquery datatables使用 jquery 数据表在 excel 导出中启用标题过滤器
【发布时间】:2016-08-15 13:05:57
【问题描述】:

我正在尝试启用对 Excel 顶行/头文件的过滤器,该文件是使用 Jquery Datatables 库从表数据生成的。

我已经按照本教程 https://datatables.net/extensions/buttons/examples/initialisation/export.html 进行了操作,效果很好,生成的 excel 文件如下所示。

正如您在该图片中看到的那样,第一行没有过滤器。我知道我们可以在打开后在excel文件中启用过滤器。但我正在尝试在生成 excel 文件时执行此操作。

因此,导出的 excel 文件应如下所示。

您可以通过过滤器看到红色圆圈中的亮点。

所以只是想知道这是否可能以及有关如何实现这一目标的任何建议将不胜感激。

谢谢。

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    您可以通过更改文件buttons.html5.js中的excel文件架构来自动启用过滤器。

    在buttons.html5.js 文件中,您可以找到以下代码。

    // Excel - Pre-defined strings to build a minimal XLSX file
    var excelStrings = {
        "_rels/.rels": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">\
        <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>\
    </Relationships>',
    
        "xl/_rels/workbook.xml.rels": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">\
        <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>\
    </Relationships>',
    
        "[Content_Types].xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
    <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">\
        <Default Extension="xml" ContentType="application/xml"/>\
        <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>\
        <Default Extension="jpeg" ContentType="image/jpeg"/>\
        <Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>\
        <Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>\
    </Types>',
    
        "xl/workbook.xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
    <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">\
        <fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="24816"/>\
        <workbookPr showInkAnnotation="0" autoCompressPictures="0"/>\
        <bookViews>\
            <workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>\
        </bookViews>\
        <sheets>\
            <sheet name="__SHEET_NAME__" sheetId="1" r:id="rId1"/>\
        </sheets>\
    </workbook>',
    
        "xl/worksheets/sheet1.xml": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
    <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">\
        <sheetData>\
            __DATA__\
        </sheetData>\
    </worksheet>'
    };
    

    您必须按如下方式更新上述对象。

    excelStrings["xl/worksheets/sheet1.xml"] = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><sheetData>__DATA__</sheetData><autoFilter ref="A1:g1"/></worksheet>';
    

    唯一的区别是我们附加了 &lt;autoFilter ref="A1:g1"/&gt;

    请注意 ref="A1:G1" 是要启用此自动过滤器的列。

    这就是我解决启用自动过滤器问题的方法。

    【讨论】:

      【解决方案2】:

      您还可以从现有的“按钮”属性访问数据表。

      buttons: [ {
              extend: 'excelHtml5',
              autoFilter: true,
              sheetName: 'Sheet Name'
          } ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-23
        • 2017-09-23
        • 1970-01-01
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        • 2020-04-03
        • 2021-12-18
        相关资源
        最近更新 更多