How to Use custom HTML and JavaScript To Export Data to Excel

1
0
-1

Hi
I have BonitaSoft BPM 7.3.1 .
I have a Custom Application Page fetching Data from an API call. The Data is displayed in a tabular format.
Now I need to export the same data as an Excel Sheet.
I need to have a button to export the Data .

I have come across several Links for this Process.
1) http://jsfiddle.net/hybrid13i/JXrwM/
2) http://stackoverflow.com/questions/16078544/export-to-csv-using-jquery-a...
3) http://jsfiddle.net/terryyounghk/KPEGU/

I am a newbie here and cannot figure out how to use these custom HTML and JavaScript in my Application page.
I have tried adding the information to a custom Widget.
HTML code in the Template and JavaScript code in the Controller section. But That doesn't Work.

Also I tried taking a TEXT field in the Page and adding the HTML Code there. Creating a JavaScript Variable .
Upon Preview, Nothing happens.

Any Guidance will be Much Appreciated.
Kindly Provide a Step by Step guide if Possible.

Also If Someone have already Created such a Page, Please share a link to download.
Thanks

13 answers

1
0
-1

Wonderfull!!! thes best code is which one you wrote on Submitted by Sean McP on Thu, 02/16/2017 - 15:54
Thanks a lot

Comments

Submitted by Dibyajit.Roy on Sat, 02/02/2019 - 20:12

Hello
One drawback is the table headers dont come in the excel.
Were you able to get the table header.
Also if you have a repeatable container then this will not work. This is applicable for table only.

Submitted by Loreley2018 on Sat, 02/02/2019 - 22:06

Hello Dibyajit.Roy
Yes you are right, I could not the table header, but at least i get only the button because i edited widget template and i only use this line:
Export Table data into Excel¨
Don´t worry about the container, it was very usefull for me.
Is it possible to get an .xlsx o .xls format?

Submitted by Sean McP on Sun, 02/03/2019 - 18:19

Hi Dibyajit and Loreley2018,

No the header isn't part of the deal. I didn't consider it at the time and haven't investigated it...I might have a look but can't promise anything.

Regarding XLS or XLSX this is not possible unless you purchase other plugins etc. CSV is a fully recognised method of downloading any data to Excel. There will be no provisioning to Excel format direct.

Regards
Seán

Submitted by Dibyajit.Roy on Sun, 02/03/2019 - 19:36

Hello Sean.
First of all, I would like to thank you for all your Comments, Posts and answers in the bonita Community.
A lot of things make sense thanks to you.. the above code works for me and has helped me several times. Its a Working model.
If you do come up with a solution to add the header in the above example, it would be great ... but its no problem as of now..
Let me know if you can guide me for a similar scenario but instead of a table widget, i have a repeatable custom container with text fields to display same as a table..
Everything is same as above.. except the API response variable is set as value to the container.

Again many many thanks for your valuable feedback.
Regards

Submitted by Loreley2018 on Mon, 02/04/2019 - 00:42

Hello Sean McP !
I agree with Dibyajit, your code Its a Working model, but i need the export the headers.
Don´t worry about that, I will continue my research.
Thanks very much

Submitted by Sean McP on Sun, 02/24/2019 - 21:27

Hi Dibyajit and Loreley2018

:) Happy news...

Not the most elegant but based on the previous code NOW HAS HEADERS!!!

Just replace the existing code with the following and it will work exactly the same way,

regards

Seán

function ($scope) {

    // define a function to be used in template with ctrl.toggleBackgroundColor()
    this.exportTableToCSV02 = function () {

        function exportTableToCSV029($table, filename) {

            var $rowsH = $table.find('tr:has(th)'),

                    // Temporary delimiter characters unlikely to be typed by keyboard
                    // This is to avoid accidentally splitting the actual contents
                    tmpColDelimH = String.fromCharCode(11), // vertical tab character
                    tmpRowDelimH = String.fromCharCode(0), // null character

                    // actual delimiter characters for CSV format
                    colDelimH = '","',
                    rowDelimH = '"\r\n"',

                    csvH = '"' + $rowsH.map(function (i, rowH) {
                        var $rowH = $(rowH),
                            $colsH = $rowH.find('th');

                        return $colsH.map(function (j, colH) {
                            var $colH = $(colH),
                                textH = $colH.text();

                            return textH.replace(/"/g, '""'); // escape double quotes

                        }).get().join(tmpColDelimH);

                    }).get().join(tmpRowDelimH)
                    .split(tmpRowDelimH).join(rowDelimH)
                    .split(tmpColDelimH).join(colDelimH) + '"';
                    
            //console.log($rowsH);
        
            var $rows = $table.find('tr:has(td)'),

                // Temporary delimiter characters unlikely to be typed by keyboard
                // This is to avoid accidentally splitting the actual contents
                tmpColDelim = String.fromCharCode(11), // vertical tab character
                tmpRowDelim = String.fromCharCode(0), // null character

                // actual delimiter characters for CSV format
                colDelim = '","',
                rowDelim = '"\r\n"',

                csvD = '"' + $rows.map(function (i, row) {
                    var $row = $(row),
                        $cols = $row.find('td');

                    return $cols.map(function (j, col) {
                        var $col = $(col),
                            text = $col.text();

                        return text.replace(/"/g, '""'); // escape double quotes

                    }).get().join(tmpColDelim);

                }).get().join(tmpRowDelim)
                .split(tmpRowDelim).join(rowDelim)
                .split(tmpColDelim).join(colDelim) + '"';

            //console.log(rowDelim + $rows);
            
            var csvS = csvH.replace(/""/,'"');
            var csvT = csvD.replace(/""/,'"');
            var csv1 = csvS + rowDelim + csvT;
            var csv2 = csv1.replace(/""/,'"');
            var csv = csv2.replace(/""/,'"');
            
            //.concat(rowDelim, csvD.replace(/""/,'"'));
            console.log("1" + csvS);
            console.log("2" + csvT);
            console.log("3" + csv1);
            console.log("4" + csv2);
            console.log("5" + csv);

            // Deliberate 'false', see comment below
            if (false && window.navigator.msSaveBlob) {

                var blob = new Blob([decodeURIComponent(csv)], {
                    type: 'text/csv;charset=utf8'
                });

                // Crashes in IE 10, IE 11 and Microsoft Edge
                // See MS Edge Issue #10396033
                // Hence, the deliberate 'false'
                // This is here just for completeness
                // Remove the 'false' at your own risk
                window.navigator.msSaveBlob(blob, filename);

            } else if (window.Blob && window.URL) {

                // HTML5 Blob
                var blob = new Blob([csv], {
                    type: 'text/csv;charset=utf-8'
                });
                var csvUrl = URL.createObjectURL(blob);

                //this trick will generate a temp  tag
                var link = document.createElement("a");
                link.href = csvUrl;

                //set the visibility hidden so it will not effect on your web-layout
                link.style = "visibility:hidden";
                link.download = filename; //fileName + ".csv";

                //this part will append the anchor tag and remove it after automatic click
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);

            } else {

                // Data URI
                var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

                //this trick will generate a temp  tag
                var link = document.createElement("a");
                link.href = csvData;

                //set the visibility hidden so it will not effect on your web-layout
                link.style = "visibility:hidden";
                link.download = filename; //fileName + ".csv";

                //this part will append the anchor tag and remove it after automatic click
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);

            }

        }

        //use a default name to export - change as necessary
        //if the table name in the div id= changes - you MUST change the name here also
        //default is dvData
        var args = [$('#dvData>table'), 'export.csv'];
        exportTableToCSV029.apply(this, args);

    };

}
Submitted by Dibyajit.Roy on Mon, 02/25/2019 - 06:57

Hello Sean
Thank you . I will try the New code.

Submitted by Loreley2018 on Tue, 02/26/2019 - 16:16

Hello Sean!!

First of all, thanks a lot for your help.

I used it, but didn't export anything. I compared your codes, and i made this change>

//var args = [$('#dvData>table'), 'export.csv']; <---I comment this line
        var args = [$('.table-responsive>table'), 'export.csv'];<---I added this one
        exportTableToCSV029.apply(this, args);

and then i got some data, but didn't work for me, because only got the 2nd and 3rd column
Sorry

        exportTableToCSV029.apply(this, args);

1
0
-1

Thank you to everyone.
Already I could solve my problem

Regards

Hugo Ramírez

1
0
-1

Hi Dibyajit.Roy!

Try using this component:
http://ui-grid.info/

To do so, create a custom widget, in the html field, put:
<div ui-grid="gridOptions" ui-grid-selection ui-grid-exporter class="grid"></div>
In the Javascript field, inside the function declaration, put:

$scope.gridOptions = {
    columnDefs: [
// put here the fields that yout data table has
      { field: 'name' }, 
      { field: 'gender', visible: false},
      { field: 'company' }
    ],
    enableGridMenu: true,
    enableSelectAll: true,
//define here the name of your exported file
    exporterCsvFilename: 'myFile.csv'
    exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
    onRegisterApi: function(gridApi){
      $scope.gridApi = gridApi;
    }

In Required angular modules field, put:
ui.grid, ui.grid.selection, ui.grid.exporter

In assets field, import this two:
https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js

https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css

At least, I recommend you to read more at http://ui-grid.info/ about this component and how to use it more efficiently.

Cheers,
Marcos Vinícius Pinto

Comments

Submitted by Dibyajit.Roy on Fri, 02/10/2017 - 16:38

Hi Marcos

Thanks for the Detailed Help.

I followed the Example. When I add the custom widget, I dont see any preview.
I must have followed some step wrong.
Could you export the Custom widget and share it

Many Thanks
Dibyajit

Submitted by mvpdev on Fri, 02/10/2017 - 17:42

Hi,
Later I'll do this.

Cheers,
Marcos Vinícius Pinto

Notifications