JavaScript-将CSV转换为XLSX(最好不使用库) [英] JavaScript - Convert CSV to XLSX (Preferably Without Use of Library(s))

查看:49
本文介绍了JavaScript-将CSV转换为XLSX(最好不使用库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我目前有一个从SharePoint列表数据创建的CSV文件,为了将此信息显示为电子表格,我想将其转换为Excel XLSX文件.我宁愿不依赖第三方库来执行此操作.最初,我开始使用ActiveX对象尝试将CS​​V重新创建和/或保存为XLSX,但是存在一个限制,因为我不能在IE之外的其他浏览器中真正使用它.我在考虑使用Blob进行某种转换吗?那就是我被困住的地方.

As the title says, I currently have a CSV file created from SharePoint list data and in order to display this information as a spreadsheet, I want to convert it to an Excel XLSX file. I prefer to do this without relying on a third-party library. At first, I started to use ActiveX objects to try to recreate and/or save the CSV as XLSX, but there's a limitation with that since I can't really use it in other browsers besides IE. I was thinking using Blob to somehow convert it? That's where I'm stuck.

function createCsv(data) {
    var result = "";

    if (data == null || data.length == 0) {
        return;
    }

    var columnDelimiter = ',';
    var lineDelimiter = '\n';

    var keys = Object.keys(data[0]);

    // spreadsheet header

    result += keys.join(columnDelimiter);
    result += lineDelimiter;

    // spreadsheet data

    data.forEach(function (obj) {
        var count = 0;

        keys.forEach(function (key) {
            if (count > 0) {
                result += columnDelimiter;
            }

            result += obj[key];
            count++;               
        });

        result += lineDelimiter;
    });

    return result;
}

function downloadCsv(csv) {
    if (csv == null) {
        return;
    }

    var filename = "test.csv";

    csv = "data:text/csv;charset=utf-8," + csv;

    var data = encodeURI(csv);

    console.log(data);

    var link = document.getElementById('csv');
    link.setAttribute('href', data);
    link.setAttribute('download', filename);

    console.log(link);

    //displayCsv(csv);
}

function displayCsv() {
    // using test csv here
    var message = "data:text/csv;charset=utf-8, yo, hey, lol";
    //var fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    var fileType = "application/msexcel";

    var csvFile = new Blob([message], {type: fileType});
    var csvUrl = URL.createObjectURL(csvFile);

    console.log(csvFile);
    console.log(csvUrl);

}

CSV与电子表格配合使用时效果很好(通过在Excel中下载并打开它),但是我确实需要一种在网页上将其显示为电子表格而不是文本的方式,所以这就是为什么我希望对其进行转换超过.由于我在SharePoint中使用此功能,因此我可以使用Excel Web部件显示XLSX-尽管它不会像这样打开CSV文件.预先感谢.

CSV works fine with using the spreadsheet (by downloading and opening it in Excel), but I really need a way to display it as a spreadsheet on a webpage and not as text, so that's why I'm looking to convert it over. Since I'm using this within SharePoint then I can use a Excel web part to display the XLSX - it won't open CSV files like this though. Thanks in advance.

推荐答案

尝试在没有库的情况下手动执行此操作将是一项相当艰巨的任务.虽然OpenXML文件以XML为核心,但它们也被捆绑/压缩.

It would be quite the undertaking to try to manually try to do this without libraries. While OpenXML files are XML based at their core, they are also bundled/zipped.

我建议您看一下SheetJS. https://sheetjs.com/

I would recommend take a look at SheetJS. https://sheetjs.com/

您可以将CSV作为输入,并立即将其写回XSLX.

You can take CSV as input, and write it back out immediately as XSLX.

这篇关于JavaScript-将CSV转换为XLSX(最好不使用库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆