user3189516
user3189516

Reputation: 73

how to export data from database into excel using ajax call + javascript

I create an application with NDWS using sapui5 _javascript. I have a table in the view, which has some data (data is sync with database on server). I want to retrieve data from the table and export in into excel (so get separate excel doc). Here is my trigger:

var oExportToExcelButton = new sap.ui.commons.Button( {
            text : "Export to Excel",
            width : '120px',
            style : sap.ui.commons.ButtonStyle.Emph,
            press : function() {

                var jsonDataObject = oController.model.getProperty("/matreqs");
                var taskIdFromView = sap.ui.getCore().byId("taskId").getValue();
                var jsonData = JSON.stringify(jsonDataObject);

                $.ajax("api/wpi/processrequest/getexcelexportfile?taskId="
                    + taskIdFromView, {
                    context : this,
                    type : "POST",
                    processData : false,
                    contentType : "application/json",
                    data : jsonData,
                    error : function(request, status, error) {
                        console.log(error);
                    },
                    success : function(data) {
                        oController.getRequestParameterValue();
                        console.log(data);
                        top.close();
                    }
                });
            }
        });

getRequestParameterValue() is a function in controller:

getRequestParameterValue: function(name) {
        var half = location.search.split("&" + name + "=")[1];

        if (!half) half = location.search.split("?" + name + "=")[1];

        return half ? decodeURIComponent(half.split("&")[0]) : null;
    })

i am very new into programming, so i am sorry if my explanation is not very clear. any help is welcome!

Upvotes: 1

Views: 2119

Answers (1)

Tim Gerlach
Tim Gerlach

Reputation: 3390

Since you already have access to the data in your Buttons press event using oController.model .getProperty("/matreqs"); on the client side you should be able to send the data to your server side Service (e.g. a Java Servlet). This should care about converting your data to a valid Excel file.

You can use Apache POI for this. This works fine for me.

If you need some more information just let me know.

Upvotes: 2

Related Questions