【问题标题】:Writing a file client side using Angularjs使用 Angularjs 编写文件客户端
【发布时间】:2016-03-10 03:59:09
【问题描述】:

我们可以在客户端使用 Angular js 编写一个 xlsx 文件吗?如果可以,请提供解决方案。

【问题讨论】:

    标签: angularjs xlsx


    【解决方案1】:

    您可以创建您的 excel 文件。

    安装npm install excel4node

    然后使用基本代码

    // Require library 
    var xl = require('excel4node');
    
    // Create a new instance of a Workbook class 
    var wb = new xl.Workbook();
    
    // Add Worksheets to the workbook 
    var ws = wb.addWorksheet('Sheet 1');
    var ws2 = wb.addWorksheet('Sheet 2');
    
    // Create a reusable style 
    var style = wb.createStyle({
        font: {
            color: '#FF0800',
            size: 12
        },
        numberFormat: '$#,##0.00; ($#,##0.00); -'
    });
    
    // Set value of cell A1 to 100 as a number type styled with paramaters of style 
    ws.cell(1,1).number(100).style(style);
    
    // Set value of cell B1 to 300 as a number type styled with paramaters of style 
    ws.cell(1,2).number(200).style(style);
    
    // Set value of cell C1 to a formula styled with paramaters of style 
    ws.cell(1,3).formula('A1 + B1').style(style);
    
    // Set value of cell A2 to 'string' styled with paramaters of style 
    ws.cell(2,1).string('string').style(style);
    
    // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size. 
    ws.cell(3,1).bool(true).style(style).style({font: {size: 14}});
    
    wb.write('Excel.xlsx');
    

    这里我添加了参考链接:https://www.npmjs.com/package/excel4node

    【讨论】:

      【解决方案2】:

      您可以使用Alasql 库将数据从 AngularJS 导出为 XLS、XLSX 和 CSV 格式。

      例如,我们想通过单击按钮将保存在测试变量中的数据写入 XLSX 文件。为此,只需使用以下步骤:

      1- 在您的页面中包含以下文件:

       <script src="http://alasql.org/console/alasql.min.js"></script> 
       <script src="http://alasql.org/console/xlsx.core.min.js"></script> 
      

      2- 然后在控制器中创建一个函数将测试变量保存为 xlsx 文件:

      $scope.test = [{a:2,b:8,c:9},{a:0,b:14,c:12},{a:24,b:35,c:76}];
      $scope.saveAsXlsx = function () {
         alasql('SELECT * INTO XLSX("output.xlsx",{headers:true}) FROM ?',[$scope.test]);
      }
      

      所以我们将变量 test 中的数据保存到我们在这里命名为 output.xlsx 的文件中。

      3- 最后一部分是最简单的部分。点击按钮运行函数:

       <button ng-click="saveAsXlsx()" >Save as XLSX</button>
      

      上面的解决方案是一个 js 库,可以与 jQuery、js 和 angularJS 一起使用。如果您正在寻找刚刚为 Angular 制作的东西,那么除了此解决方案之外,您还可以使用 ng-csv 库。如果您只想拥有一个电子表格,您也可以使用ng-csv 库。这很容易,但您无法获得 XLSX,您将获得 CSV 格式的电子表格文件。

      【讨论】:

      【解决方案3】:

      您可以使用 angular-xlsxjs-xlsx 之类的库来帮助您完成此过程。

      【讨论】:

        猜你喜欢
        • 2011-07-08
        • 2011-07-28
        • 2011-03-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        相关资源
        最近更新 更多