如何在使用 JavaScript 将数据下载为 xls 格式时订购 json 数据 header

how to order json data header while downloading it into xls format using JavaScript

我有一个 Json 数据,我在其中生成具有财政年度季度和相应值的动态键,我需要将数据下载为 xls 格式,我可以成功地做到这一点,但是问题是当我下载数据时 xls header 的顺序与我的 json 不同 keys.Below 是我的示例数据。

var input = [
    {
        "FPH Level 1": "iphone",
        "Geo Level 2": "Austria",
        "Geo Level 7": "DACH",
        "RTM": "Retail",
        "Account": "Austria-epos",
        "FY202004": "20%",
        "FY202101": "20%",
        "FY202102": "20%",
        "FY202103": "20%",
        "FY202104": "20%",
        "Y/Y pt Change": "5%",
        "Commentary Y/Y": "TESTING",
        "Q/Q pt Change": "4%",
        "Commentary Q/Q": "TESTING"
    },
    {
        "FPH Level 1": "iphone",
        "Geo Level 2": "Austria",
        "Geo Level 7": "DACH",
        "RTM": "Retail",
        "Account": "Austria-epos",
        "FY202004": "20%",
        "FY202101": "20%",
        "FY202102": "20%",
        "FY202103": "20%",
        "FY202104": "20%",
        "Y/Y pt Change": "5%",
        "Commentary Y/Y": "TESTING",
        "Q/Q pt Change": "4%",
        "Commentary Q/Q": "TESTING"
    },
    {
        "FPH Level 1": "iphone",
        "Geo Level 2": "Austria",
        "Geo Level 7": "DACH",
        "RTM": "Retail",
        "Account": "Austria-epos",
        "FY202004": "20%",
        "FY202101": "20%",
        "FY202102": "20%",
        "FY202103": "20%",
        "FY202104": "20%",
        "Y/Y pt Change": "5%",
        "Commentary Y/Y": "TESTING",
        "Q/Q pt Change": "4%",
        "Commentary Q/Q": "TESTING"
    },
    {
        "FPH Level 1": "iphone",
        "Geo Level 2": "Austria",
        "Geo Level 7": "DACH",
        "RTM": "Retail",
        "Account": "Austria-epos",
        "FY202004": "20%",
        "FY202101": "20%",
        "FY202102": "20%",
        "FY202103": "20%",
        "FY202104": "20%",
        "Y/Y pt Change": "5%",
        "Commentary Y/Y": "TESTING",
        "Q/Q pt Change": "4%",
        "Commentary Q/Q": "TESTING"
    },
    {
        "FPH Level 1": "iphone",
        "Geo Level 2": "Austria",
        "Geo Level 7": "DACH",
        "RTM": "Retail",
        "Account": "Austria-epos",
        "FY202004": "20%",
        "FY202101": "20%",
        "FY202102": "20%",
        "FY202103": "20%",
        "FY202104": "20%",
        "Y/Y pt Change": "5%",
        "Commentary Y/Y": "TESTING",
        "Q/Q pt Change": "4%",
        "Commentary Q/Q": "TESTING"
    },
]

在这里截取代码我正在下载数据

const xlsData = input
        const ws = XLSX.utils.json_to_sheet(xlsData);
        const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };
        const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
        const data = new Blob([excelBuffer], { type: fileType });
        let fileName = `test`
        FileSaver.saveAs(data, fileName + fileExtension);

转成xls后的结果header是这样的 我排除了输出 be

在您的代码片段中,将第二行更改为:

const header = ["FPH Level 1", "Geo Level 2", "Geo Level 7", "RTM", "Account"]
const fy = Object.keys(input[0]).filter(s => s.startsWith("FY")).sort()
header.push(...fy)
header.push("Y/Y pt Change", "Commentary Y/Y", "Q/Q pt Change", "Commentary Q/Q")
const ws = XLSX.utils.json_to_sheet(xlsData, { header })

你可以试一试!

// This part converts your original object into an array
let data_arr = [...input].reduce((acc, val) => {
    acc.push(Object.values(val))
    return acc
}, [])

// The array is feed here
hdl.addEventListener('click', function() {
    var wb = XLSX.utils.book_new();

    wb.Props = {
        Title: window.sheet_title,
        Subject: "Sheet Subject",
        Author: "Name of author",
        CreatedDate: new Date(window.page_time)
    };

    wb.SheetNames.push("Sheet Subject");

    var ws = XLSX.utils.aoa_to_sheet(data_arr);
    wb.Sheets["Sheet Subject"] = ws;

    var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        type: 'binary'
    });

    function s2ab(s) {

        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;

    }

    saveAs(new Blob([s2ab(wbout)], {
        type: "application/octet-stream"
    }), `${window.sheet_title}.xlsx`);
})