cancel
Showing results for 
Search instead for 
Did you mean: 

xsjs excel File Download Problem

prkash_s
Participant
0 Kudos

hi ,

Im Trying to create an App in SAP hana and UI5 , an Purchase Order App .

in that im getting an issue .

when i click button for Excel Download. file was not downloading but if u run the link seperately or Double click in Developer tool in chorme browser it working

my Code :

My.XSJS

$.import("sap.hana.democontent.epm.services", "messages");

var MESSAGES = $.sap.hana.democontent.epm.services.messages;

function downloadExcel() {

                      var body = '';

                         try {

   

                         var query = 'SELECT TOP 25000 \"PurchaseOrderId\", \"PartnerId\",\"CompanyName\", \"CreatedByLoginName\", \"CreatedAt\",                         \"GrossAmount\"'+ 'FROM\"sap.hana.democontent.epm.data::purchaseOrderHeaderExt\" order by \"PurchaseOrderId\"';

 

          $.trace.debug(query);

 

          var conn = $.db.getConnection();

          var pstmt = conn.prepareStatement(query);

          var rs = pstmt.executeQuery();

 

          body = MESSAGES.getMessage('SEPM_POWRK', '002') + "\t" + //          PurchaseOrder ID

          MESSAGES.getMessage('SEPM_POWRK', '003') + "\t" + // Partner ID

          MESSAGES.getMessage('SEPM_POWRK', '001') + "\t" + // Company Name

          MESSAGES.getMessage('SEPM_POWRK', '004') + "\t" + // Employee Responsible

          MESSAGES.getMessage('SEPM_POWRK', '005') + "\t" + // Created At

          MESSAGES.getMessage('SEPM_POWRK', '006') + "\n"; // Gross Amount

          while (rs.next()) {

          body += rs.getNString(1) + "\t" + rs.getNString(2) + "\t"

          + rs.getNString(3) + "\t" + rs.getNString(4) + "\t"

          + rs.getDate(5) + "\t" + rs.getDecimal(6) + "\n";

          }

          } catch (e) {

          $.response.status = $.net.http.INTERNAL_SERVER_ERROR;

          $.response.setBody(e.message);

          return;

          }

          $.response.setBody(body);

 

          // $.response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

          // $.response.AppendHeader("content-disposition","attachment;filename=Product_Report.xls");

          

          $.response.contentType = 'application/vnd.ms-excel;charset=utf-16le';

          $.response.headers.set("content-disposition","attachment; filename=Excel.xls");

          $.response.headers.set("access-control-allow-origin","*");

          //$.response.status = $.net.http.OK;

          $.response.setReturnCode($.net.http.OK);   

}

var aCmd;

var aCmd = $.request.parameters.get('cmd');

switch (aCmd) {

  case "Excel":

                                        downloadExcel();

                                        break;

 

default:

                                          $.response.status = $.net.http.INTERNAL_SERVER_ERROR;

                                        $.response.setBody('Invalid Command: '+aCmd);

                     }

-----------------------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------------------

     Controller .js

 

  downloadExcel1 : function () {

                         alert("Download Excel");

                         var aUrl = '../../../services/My.xsjs?cmd=Excel'; 

                                 

                         jQuery.ajax({   

                                                              url: aUrl,   

                                                              method: 'GETT',   

                                                              dataType: 'json',   

                                                              success: this.onCompleteMultiply,   

                                                              error: this.onErrorCall });   

                                                                            },

                     

                     

               onErrorCall: function(jqXHR, textStatus, errorThrown){   

                               

                 sap.ui.commons.MessageBox.show(jqXHR.responseText,"ERROR", "Error in calling Service" );

                   

                return;   

            },

                     

               onCompleteMultiply: function(mt) {   

                          var mResult = sap.ui.getCore().byId("result");   

                           if(mt==undefined){ mResult.setText(0); }   

                           else{    

                             jQuery.sap.require("sap.ui.core.format.NumberFormat");   

                             var oNumberFormat = sap.ui.core.format.NumberFormat.getIntegerInstance({   

                                maxFractionDigits: 10,   

                                minFractionDigits: 0,   

                                groupingEnabled: true });   

                             mResult.setText(oNumberFormat.format(mt));

                 }                         

               },

              

-----------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------

   view.js

      var btn_expExcel = new sap.ui.commons.Button("btn_expExcel");  

      btn_expExcel.setText("Export to Excel");  

      btn_expExcel.attachPress(oController.downloadExcel1);

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Prkash,

Were you able to resolve this issue. I am having the same issue.

Thank you.