使用Angularjs编写文件客户端

时间:2015-12-05 05:14:14

标签: angularjs xlsx

我们可以使用角度js在客户端编写xlsx文件。如果可以,请提供解决方案。

3 个答案:

答案 0 :(得分:1)

您可以使用angular-xlsxjs-xlsx等图书馆来帮助您完成此过程。

答案 1 :(得分:0)

您可以使用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- So然后在控制器中创建一个函数,将测试变量保存为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]);
}

所以我们将变量测试中的数据保存到一个文件中,我们在这里将其命名为output.xlsx。

3-最后一部分是最简单的部分。单击按钮运行该功能:

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

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

答案 2 :(得分:0)

您可以创建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