jquery 我无法在Visual Studio.NET中包含XLSX.Read库

zu0ti5jz  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(121)
function importExcel() {

    sAlert("importExcel");

    var fileDialog = $('<input type="file">');
    fileDialog.click();
    fileDialog.on("change", onFileSelected);
    return false;
};

var onFileSelected = function (e) {
    //var filePath = $(this).val();

    if (this.files.length > 0) {
        var file = this.files[0];
        var reader = new FileReader();
        sAlert("file " + file.name);
    }

    reader.onload = function (e) {
        sAlert("reader.onload ");
        var data = new Uint8Array(e.target.result);
        sAlert("data " + data);
        //const XLSX = xlsx;
        //var workbook = XLSX.read(data, { type: 'array' }); 
        var XLSX = require("xlsx");
        var workbook = XLSX.read(data, { type: 'array' }); 
        sAlert("workbook " + workbook.name);
        var formulario = workbook.SheetNames['Formulario'];
        sAlert("worksheet " + formulario.name);
        var worksheet = wb.Sheets[formulario];        
        //var worksheet = workbook.Sheets[workbook.SheetNames['Formulario']];
        sAlert("worksheet " + worksheet.name);
        // Agora você pode trabalhar com os dados da planilha
        var jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
        sAlert(jsonData);
        //console.log(jsonData);

        // Exemplo de como acessar uma célula específica
        var cellValue = worksheet['A1'].v;
        sAlert(cellValue);
        //console.log(cellValue);
    };

    sAlert("readAsArrayBuffer " + file.name);
    reader.readAsArrayBuffer(file);
    
    //sAlert($(this)[0].files);
};

我想包括XLSX.read库,但我不知道如何,有人可以帮助我吗?

m528fe3b

m528fe3b1#

你可以从github下载:https://github.com/SheetJS/sheetjs
之后包括在您的项目中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Page Title</title>
</head>
<body>
    <!-- Include the xlsx library -->
    <script src="path-to-xlsx/xlsx.full.min.js"></script>
    <!-- Your JavaScript code goes here -->
    <script src="your-script.js"></script>
</body>
</html>

Chnage src地址与您的。
使用方法如下:

var workbook = XLSX.read(data, { type: 'array' });

相关问题