Skip to Content
0
Oct 01, 2019 at 12:58 PM

Download an Excel File in SAP UI5 XML controller

122 Views

hi Team,

I have a Java servlet , which basically fetches data from the Oracle DataBase.

From the UI5 applications of type xml view and xml controller, am using Ajax Post method for executing the servlet and fetching data in Json format.

Now we have a requirement where we have to download the excel document from the servlet.

At servelt am generating the data as well as putting them into excel file. Please see the below code.

byte[] l_byteArray_ExcelFileData = downloadReport_ToExcel(g_out, "", "", l_str_Input_InJsonFormat);


l_HttpServletResponse_servletResponse.setHeader("Content-disposition", "attachment;filename=" + l_str_fileName);
l_HttpServletResponse_servletResponse.setHeader("charset", "iso-8859-1");
l_HttpServletResponse_servletResponse.setContentType(l_str_contentType);
l_HttpServletResponse_servletResponse.setContentLength(l_byteArray_ExcelFileData.length);
l_HttpServletResponse_servletResponse.setStatus(HttpServletResponse.SC_OK);


OutputStream l_outputStream = null;
try {


l_outputStream = l_HttpServletResponse_servletResponse.getOutputStream();
l_outputStream.write(l_byteArray_ExcelFileData, 0, l_byteArray_ExcelFileData.length);
l_outputStream.flush();
l_outputStream.close();
l_HttpServletResponse_servletResponse.flushBuffer();
} catch (IOException e)
{
throw new RuntimeException(e);
}

and At UI5 xml controller we are usign the below ajax post code for executing the servlet.

var l_input_injsonformat = {getting input data here in json format using JSON.Stringfy};
var aData = jQuery.ajax
          			 ({
          		           type : "POST",					        
          		           url : l_var_url,	 					          		    	          data:l_input_injsonformat, 
          		      beforeSend:  function(jqXHR, settings) 
           		     {
           		     },
          		     error: function (xhr, ajaxOptions, thrownError) 
          		     {
 	           		    	console.log(xhr.message);
	           		    	console.log(xhr.status);
	           		    	console.log(xhr.responseText);
           		     },
          	        async: true, 
          	        success : function(data,textStatus, jqXHR) 
          	        { 
          	          var charset = "utf-8";
			  var filename = "search_results.csv";
			  var blob = new Blob([data], 
			  {
			    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"
			  });
          	        }
          });

am unable to download the file. Kindly do the Need Full.

Regards

Govardan