Below are the methods I have written to generate excel file and send it as a download option. but I am getting some junk data as response. Controller method:
@RequestMapping(value="/loanaccounts/repaymentScheduleInExcel", method = RequestMethod.GET,produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@Timed
public void repaymentScheduleInExcel(@RequestParam(value = "accountNumber", required = true) String accountNumber, HttpServletResponse response)throws Exception, URISyntaxException {
log.debug("REST request to do unmarkNPA");
loanProcessService.repaymentScheduleInExcel(accountNumber, response);
}
Implemenation method:
public void repaymentScheduleInExcel(String accountNumber,HttpServletResponse response) {
try {
response.setHeader("Content-Disposition","attachment; filename="+ accountNumber+ ".xlsx");
response.setHeader("cache-control", "no-cache");
response.setDateHeader("Last-Modified", System.currentTimeMillis());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
String file = "D://text.xlsx";
FileOutputStream f = new FileOutputStream(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("repayment_schedule_details");
String[] headers = {"Sequence Num","Transaction Name","Demand Date","Transaction Date","Installment Amount","Balance Amount","Interest Due","Principle Due"};
Row headerRow = sheet.createRow(0);
for(String header : headers){
int j = 0;
Cell cell = headerRow.createCell(j++);
cell.setCellValue(header);
}
LoanOdSummaryWSDto transactionSummary = encoreService.findSummary(accountNumber, true);
if(transactionSummary != null && transactionSummary.getRepaymentSchedule() != null && transactionSummary.getRepaymentSchedule().size()>0){
List<TransactionSummaryWSDto> repaymentSchedules = transactionSummary.getRepaymentSchedule();
for(TransactionSummaryWSDto repaymentSchedule : repaymentSchedules){
int rowNumber = 1;
Row valueRow = sheet.createRow(rowNumber++);
int columnNumber = 0;
Cell seqNum = valueRow.createCell(columnNumber++);
seqNum.setCellValue(repaymentSchedule.getSequenceNum());
Cell transactionName = valueRow.createCell(columnNumber++);
transactionName.setCellValue(repaymentSchedule.getTransactionName());
Cell valueDate = valueRow.createCell(columnNumber++);
valueDate.setCellValue(repaymentSchedule.getValueDate());
Cell transactionDate = valueRow.createCell(columnNumber++);
transactionDate.setCellValue(repaymentSchedule.getTransactionDate());
Cell amount = valueRow.createCell(columnNumber++);
amount.setCellValue(repaymentSchedule.getAmount1());
Cell amount2 = valueRow.createCell(columnNumber++);
amount2.setCellValue(repaymentSchedule.getAmount2());
Cell part1 = valueRow.createCell(columnNumber++);
part1.setCellValue(repaymentSchedule.getPart1());
Cell part2 = valueRow.createCell(columnNumber++);
part2.setCellValue(repaymentSchedule.getPart2());
}
}
workbook.write(f);
workbook.close();
System.out.println("Excel written successfully..");
} catch (Exception e) {
throw new ProgramException("encore call failure for given account number");
}
}
Output:
PK 3��J _rels/.rels���J1���Pr��� "�u/"�Md}��ff�L��F���E]w@�c���?H��9L�r� ��E�������nu�F�G2v7�g��P�N|�Bb10��k��(`i8Q���s@�e�uB;bOzӶ�:e�1U흁�wkP�=��yү��'汩�:xK��X�:o��s�('ҏ^�^��|�8����.���:4EGn�je�T��.N8Y��7����@�?�?����P;�PK�A��� U PK 3��J [Content_Types].xml�SMO1���6��m��1���G%@mgن~����cDH0��t��:iG㕳�R6�7l����6~ְ��s}˪��ki����������:B�h��u��N��:p2��҆�$R�f"J5�3׃��P�#x��h����\X�6Ho���%�����;��V�'�='w&�+"��iE���8�cw����+�' ����(�A-�p(�t�f�Ȅ/ґ� ��,H�����eY�'z��8�R� �幓 �&zXc���E8k\�=�(z�[�ʝ4~��gH���LP<�� =�E_�?ID����PK�c��@ 2 PK 3��J docProps/app.xmlM�� �0D��ro�z�4� �'{����MHV�盓z���T��E�1e�����Ɇ�ѳ����:�No7jH!bb�Y��V��������T�)$o���0M��9ؗGb�7�pe��*~�R�>��Y�EB���nW������ PK6n�!� � PK 3��J docProps/core.xmlm��J�0F�ߡ依�w �.�,�V�B2���$���M�k�rf�f��������r��F����c�K�$�k���"ڐm}~�� �p�Ez�Iiτ�H�e ^t���"����8�C,]��7�"y~ ��ǧ���H��Or&�2�
mL�{�T^]7;R9]��*�놖�RV/��T+���J����f"�v��Oر�PKq� � PK 3��J xl/sharedStrings.xmlm��J1���Pzw;z�E:]�A��"�>@�dw m:6���vDw��|ɟ?��|��>�p����U