【问题标题】:Read Xlsx and csv file in angularjs在 angularjs 中读取 Xlsx 和 csv 文件
【发布时间】:2018-05-28 09:10:00
【问题描述】:

我需要在angularjs中读取xlsx和csv文件的每一行,并获取json格式的数据。

目前我正在使用angular-file-upload 来获取这样的文件。

$scope.LatLongUploader = new FileUploader({
    //url: "/Map/UploadDamageRatioFile",
    queueLimit: 1,
    removeAfterUpload: true
});

$scope.locationUpload = function () {

        console.log( $scope.LatLongUploader)
        console.log( $scope.LatLongUploader.queue[0].file)
}

在我的 html 中:

      <div ng-show="true" style="margin-top:4px">
      <input style=" width:212px; float:left" type="file" nv-file-select uploader="LatLongUploader" accept=".xlsx" class="AddLocationUpload" />
      <button ng-click="locationUpload()" ng-disabled=""  id="uploadlatlongfile" title="Upload Latitude Longitude File"><i class="fa fa-upload"></i></button></div>

现在我无法继续处理这个文件,比如 object。我找到了几种方法来读取这个文件,比如 thisthis。但是这些都不起作用。我发现了一些 github 项目,比如 this_1 和 @987654325 @。

我需要有关最佳做法的帮助?

我应该在我的服务器端代码中读取这个文件并将数据发送到客户端吗?

哪个第三方应用程序/小部件适合此用途?

【问题讨论】:

    标签: javascript angularjs excel csv readfile


    【解决方案1】:

    最后我设法得到这样的文件......

    $scope.getLatLonGFile = function () {
            var FileUpload = document.getElementsByClassName('AddLocationUpload');
            $scope.file = FileUpload[0].files[0];
    
            for (var i = 0; i < FileUpload.length; i++) {
                FileUpload[i] = function () {
                    $scope.file = this.files[0];
                }
            }
        } 
    

    现在您可以通过$scope.file轻松读取文件并根据需要获取数据。

    谢谢

    【讨论】:

    • 为什么不考虑在 $scope.file 中推送数据而不是每次都替换? // 比如:$scope.file.push(this.files[0])
    • 但是我为什么要把这些数据保留在我不需要的范围内......我认为这取决于你有什么 scnerio..
    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 2022-11-16
    • 2017-06-06
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2015-09-30
    相关资源
    最近更新 更多