Angular JS解析Excel

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 Angular JS解析Excel,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Angular JS解析Excel

https://www.aspsnippets.com/Articles/Read-Excel-File-using-AngularJS-and-HTML5-File-API.aspx
https://www.cnblogs.com/liuxianan/p/js-excel.html

<input type="file" class="uploadButton" id="uploadZjUserFile"/>
<input type="button" class="submitButton" value="导入" ng-click="uploadFile(3)"/>
const excelSuffix = ['xlsx', 'xls'];
	// 上传文件
	$scope.uploadFile = function (fieldType) {
    let file;
    if (fieldType === 1) {
        file = document.getElementById("uploadGroupFile").files[0];
        $scope.uploadFileWay = true;
    } else if (fieldType === 2) {
        file = document.getElementById("uploadZxUserFile").files[0];
    }
    if (file == null) {
        new hullabaloo().send("导入失败", "请选择文件", "danger");
    }
    if (excelSuffix.includes(file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase())) {
        if (typeof (FileReader) != "undefined") {
            const reader = new FileReader();
            // For Browsers other than IE.
            if (reader.readAsBinaryString) {
                reader.onload = function (e) {
                    $scope.processExcel(e.target.result, fieldType);
                };
                reader.readAsBinaryString(file);
            } else {
                // For IE Browser.
                reader.onload = function (e) {
                    let data = "";
                    const bytes = new Uint8Array(e.target.result);
                    for (let i = 0; i < bytes.byteLength; i++) {
                        data += String.fromCharCode(bytes[i]);
                    }
                    $scope.processExcel(data, fieldType);
                };
                reader.readAsArrayBuffer(file);
            }
        } else {
            $scope.clearFileUpload();
            new hullabaloo().send("导入失败", "当前浏览器不支持H5.", "danger");
        }
    } else {
        $scope.clearFileUpload();
        new hullabaloo().send("导入失败", "请上传Excel文件", "danger");
    }
};

// Excel解析
$scope.processExcel = function (data, fieldType) {
    // Read the Excel File data.
    const workbook = XLSX.read(data, {
        type: 'binary'
    });
    // Fetch the name of First Sheet.
    const firstSheet = workbook.SheetNames[0];
    // Read all rows from First Sheet into an JSON array.
    const excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);
    // Display the data from Excel file in Table.
    $scope.$apply(function () {
        const result = $scope.parseRow(excelRows, fieldType);
        if (result.includes(undefined)) {
            $scope.clearFileUpload();
            new hullabaloo().send("导入失败", "导入数据有误,请对照示例检查", "danger");
            return;
        }
        if (fieldType === 1) {
            // 组别
            $scope.seargropname2 = result;
        } else if (fieldType === 2) {
            // 坐席
            $scope.searchZxUser = result;
        }
    });
};

// 获取数据
$scope.parseRow = function (rows, fieldType) {
    const result = [];
    for (let i = 0; i < rows.length; i++) {
        if (fieldType === 1) {
	        // column name in first row
            result.push(rows[i].组别);
        } else if (fieldType === 2) {
            result.push(rows[i].坐席);
        }
    }
    return result;
};

// 清空
$scope.clearFileUpload = function() {
    document.getElementById("uploadGroupFile").value = '';
    document.getElementById("uploadZxUserFile").value = '';
};

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142250.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!