[英]Exporting ng-grid data to CSV and PDF format in angularjs
请帮助我....任何插件都在那里..?
我已经在angularjs中搜索过excel和PDF。 使用ng-grid。
将angular网格数据导出为angularjs中的CSV和PDF格式
您现在不需要任何外部插件。 ng grid现在调用新版本UI-Grid具有本机支持。 方法名称是csvExport和pdfExport。
如果你能够做一些角度以外的事情你可以使用https://github.com/Ziv-Barber/officegen for excel。 有关pdf,请参见https://stackoverflow.com/questions/18476921/angularjs-generating-a-pdf-client-side 。
我用过jsPDF 。 这是最简单的。
将其包含在您的html
:
<script src="jspdf.min.js"></script>
<!-- script src="jspdf.debug.js"></script--><!-- for development -->
使用它1 :
var doc = new jsPDF();
doc.text(20, 20, 'Hello world.');
doc.save('Test.pdf');
并将您的按钮或任何内容绑定到此代码。
高级提示
我还发现jsPDF-AutoTable插件-for-jsPDF非常有用。
将其包含在您的html
:
<script src="jspdf.plugin.autotable.js"></script>
在controller
,使用jsPDF-AutoTable插件将数据从ng-grid
数据传输到jsPDF。
假设您定义了ng-grid
表:
$scope.gridOptions = {
data: 'myData',
columnDefs: [
{field: 'user', displayName: 'User' /*,...*/ },
{field: 'email', displayName: 'Email' /*,...*/ },
{field: 'favoriteShruberry', displayName: 'Favorite Shrubbery' /*,...*/ }
]
};
...然后,在生成pdf
的函数中:
var columns = [];
var rows = [];
// copy ng-grid's titles to pdf's table definition:
var allColumnDefs = $scope.gridOptions.columnDefs;
for ( var columnIdx in allColumnDefs ) {
var columnDef = allColumnDefs[columnIdx];
var newColumnDef = {
title: columnDef.displayName,
dataKey: columnDef.field
};
columns.push(newColumnDef);
}
// copy ng-grid's actual data to pdf's table:
var allRecords = $scope.myData;
for ( var recordIdx in allRecords ) {
var record = allRecords[recordIdx];
var newRow = {};
for ( var columnIdx in allColumnDefs ) {
var columnDef = allColumnDefs[columnIdx];
var value = record[columnDef.field];
if (value !== null) {
newRow[columnDef.field] = value;
}
}
rows.push(newRow);
}
var docName = 'favoriteShrubberies.pdf';
var pdfStyle = { styles: { overflow: 'linebreak' } } // this handles cells that contain really long text like in this comment, by auto-inserting a
// line break inside the cell, causing the whole line's height to increase accordingly
var doc = new jsPDF('p', 'pt'); // currently supports only 'pt'
doc.autoTable(columns, rows, pdfStyle);
doc.save(docName);
1例子直接来自jsPDF GitHub repo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.