【问题标题】:How to create csv or Excel file using javascript in Cordova android app如何在 Cordova android 应用程序中使用 javascript 创建 csv 或 Excel 文件
【发布时间】:2016-03-15 15:08:27
【问题描述】:

我正在尝试使用 phoneGap 创建 android 应用程序。我已经编写了用于在 javascript 中创建 csv 文件的代码,该代码在浏览器上运行良好,但在使用 cordova 创建的移动应用程序中无法运行。除了文件创建之外的所有内容在 cordova app.Pl 中都运行良好。指导

我在清单中使用了以下权限,

创建 csv 文件的代码:- var link = document.getElementById("dataLink");

        var csv = "";
            //we should have the same amount of name/cookie fields

                var name = "testdata1";
                var cookies = "testdata2",
                csv = csv + ""+ name + "," + cookies + "\n";    

                    console.log("csv-"+csv);

                    $("#dataLink").attr("href", 'data:Application/octet-stream,' + encodeURIComponent(csv))[0].click();

【问题讨论】:

    标签: phonegap-plugins phonegap-build cordova-plugins export-to-csv cordova-2.0.0


    【解决方案1】:

    您需要使用 Crodova 的 File Plugin 将文件写入文件系统,因为不同的操作系统只有某些目录可供应用程序读取/写入。

    创建文件对象和编写的代码如下所示

    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
       console.log("got main dir", dir);
       dir.getFile("myfile.csv", {
         create: true
       }, function(file) {
         console.log("got the file", file);
         logOb = file;
         var csv = "";
         //we should have the same amount of name/cookie fields
         var name = "testdata1";
         var cookies = "testdata2",
           csv = csv + "" + name + "," + cookies + "\n";
         console.log("csv-" + csv);
         writeLog(csv);
       });
     });
    
     function writeLog(str) {
       if (!logOb) return;
       logOb.createWriter(function(fileWriter) {
         fileWriter.seek(fileWriter.length);
    
         var blob = new Blob([str], {
           type: 'text/plain'
         });
         fileWriter.write(blob);
         console.log("ok, in theory i worked");
       }, fail);
     }
    

    您可以参考this tutorial更好地了解文件写入的过程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多