简体   繁体   English

将angular网格数据导出为angularjs中的CSV和PDF格式

[英]Exporting ng-grid data to CSV and PDF format in angularjs

Please Help me....any plugin is there..? 请帮助我....任何插件都在那里..?

I have searched for exporing excel and PDF in angularjs. 我已经在angularjs中搜索过excel和PDF。 using ng-grid. 使用ng-grid。

Exporting ng-grid data to CSV and PDF format in angularjs 将angular网格数据导出为angularjs中的CSV和PDF格式

For csv export there is the ngGridCsvExportPlugin that you can find here 对于csv导出,您可以在此处找到ngGridCsvExportPlugin
Just at a reference to the script and add the ngGridCsvExportPlugin to the gridOptions 只需参考脚本并将ngGridCsvExportPlugin添加到gridOptions
(and activate the footer too by adding showFooter : true to the gridOption) (并通过向gridOption添加showFooter:true来激活页脚)

 $scope.gridOptions = {
        data: 'myData',
        plugins: [new ngGridCsvExportPlugin()],
        showFooter: true,
      };

A basic plunker where you can see it at work can be found here 您可以在这里找到可以在工作中看到它的基本插件

You don't need any external plugin now. 您现在不需要任何外部插件。 ng grid which new version is called now UI-Grid has native support. ng grid现在调用新版本UI-Grid具有本机支持。 Method names are csvExport and pdfExport. 方法名称是csvExport和pdfExport。

http://ui-grid.info/docs/#/tutorial/206_exporting_data http://ui-grid.info/docs/#/tutorial/206_exporting_data

If you are able to do something outside of angular you could use https://github.com/Ziv-Barber/officegen for excel. 如果你能够做一些角度以外的事情你可以使用https://github.com/Ziv-Barber/officegen for excel。 See here https://stackoverflow.com/questions/18476921/angularjs-generating-a-pdf-client-side for pdfs. 有关pdf,请参见https://stackoverflow.com/questions/18476921/angularjs-generating-a-pdf-client-side

I used jsPDF . 我用过jsPDF It's the simplest ever. 这是最简单的。

Include it in your html : 将其包含在您的html

<script src="jspdf.min.js"></script>
<!-- script src="jspdf.debug.js"></script--><!-- for development -->

Use it 1 : 使用它1

var doc = new jsPDF();
doc.text(20, 20, 'Hello world.');
doc.save('Test.pdf');

And bind your button or whatever to this code. 并将您的按钮或任何内容绑定到此代码。


Advanced Tip 高级提示

I also found the jsPDF-AutoTable plugin-for-jsPDF extremely useful. 我还发现jsPDF-AutoTable插件-for-jsPDF非常有用。

Include it in your html : 将其包含在您的html

<script src="jspdf.plugin.autotable.js"></script>

In the controller , transfer data from ng-grid data to jsPDF, using jsPDF-AutoTable plugin. controller ,使用jsPDF-AutoTable插件将数据从ng-grid数据传输到jsPDF。

Assuming you define your ng-grid table: 假设您定义了ng-grid表:

    $scope.gridOptions = {
        data: 'myData',
        columnDefs: [
                {field: 'user', displayName: 'User' /*,...*/ },
                {field: 'email', displayName: 'Email' /*,...*/ },
                {field: 'favoriteShruberry', displayName: 'Favorite Shrubbery' /*,...*/ }
         ]
    };

... Then, in the function that generates the pdf : ...然后,在生成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 Example is straight from the jsPDF GitHub repo 1例子直接来自jsPDF GitHub repo

Very late to this party, but I wrote a PDF output that works for me. 这个派对很晚,但我写了一个适合我的PDF输出。 There is a plunker , and it is available as a plugin for V2 of ng-grid, but it doesn't look like they have taken it through into V3 (but I just had a very quick peek, so I could be wrong). 有一个plunker ,它可以作为ng-grid的V2的插件 ,但它看起来不像是它已经通过V3(但我只是有一个非常快速的偷看,所以我可能是错的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM