如何在保留相同订单的同时使用XLSX写入Excel文件

发布于 2025-02-10 19:55:53 字数 926 浏览 1 评论 0原文

这是我写给表格的代码。

const handleClick = () => {
    var wb = XLSX.utils.book_new();
    var ws = XLSX.utils.json_to_sheet(arrayofobjects);
    XLSX.utils.book_append_sheet(wb, ws, 'Test');
    XLSX.writeFile(wb, 'file.xlsx');
};

let arrayofobjects = [
    {
        "00": 4,
        "01": 6,
        "10": 12,
        "11": 7,
    },
    {
        "00": 7,
        "01": 7,
        "10": 4,
        "11": 5,
    }
]

但我在Excel

”首先,我获得10,11之后,我得到00,01”

这就是我期望的

Here is my code that writes to the sheet.

const handleClick = () => {
    var wb = XLSX.utils.book_new();
    var ws = XLSX.utils.json_to_sheet(arrayofobjects);
    XLSX.utils.book_append_sheet(wb, ws, 'Test');
    XLSX.writeFile(wb, 'file.xlsx');
};

let arrayofobjects = [
    {
        "00": 4,
        "01": 6,
        "10": 12,
        "11": 7,
    },
    {
        "00": 7,
        "01": 7,
        "10": 4,
        "11": 5,
    }
]

But I am getting output in another order in excel

first I am getting 10,11 later I am getting 00,01

This is what I am expecting

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-02-17 19:55:53

您缺少的是按期望顺序的标头数组。

例如

// defined array of your headers
const header = ["00", "01", "10", "11"]

// create workbook as usual
const wb = XLSX.utils.book_new();

// here you pass an additional variable called header
const ws = XLSX.utils.json_to_sheet(
    arrayofobjects,
    {header:header}, //you can also use {header} but for simplicity's sake i left it as is
);

What you are missing is a header array in the desired order.

for instance

// defined array of your headers
const header = ["00", "01", "10", "11"]

// create workbook as usual
const wb = XLSX.utils.book_new();

// here you pass an additional variable called header
const ws = XLSX.utils.json_to_sheet(
    arrayofobjects,
    {header:header}, //you can also use {header} but for simplicity's sake i left it as is
);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文