使用 Angularjs 编写文件客户端
Writing a file client side using Angularjs
我们可以在客户端使用 angular js 编写一个 xlsx 文件吗?如果可以,请提供解决方案。
您可以使用 angular-xlsx or js-xlsx 这样的库来帮助您完成此过程。
您可以使用 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 格式的电子表格文件。
您可以创建 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');
这里我添加了 ref link :https://www.npmjs.com/package/excel4node
我们可以在客户端使用 angular js 编写一个 xlsx 文件吗?如果可以,请提供解决方案。
您可以使用 angular-xlsx or js-xlsx 这样的库来帮助您完成此过程。
您可以使用 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 格式的电子表格文件。
您可以创建 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');
这里我添加了 ref link :https://www.npmjs.com/package/excel4node