Java SpringExcel下载返回文件中的随机数据

Java SpringExcel下载返回文件中的随机数据,java,spring,spring-mvc,Java,Spring,Spring Mvc,下面是我编写的生成excel文件并将其作为下载选项发送的方法。 但我得到了一些垃圾数据作为回应。 控制器方法: @RequestMapping(value="/loanaccounts/repaymentScheduleInExcel", method = RequestMethod.GET,produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") @Timed public void rep

下面是我编写的生成excel文件并将其作为下载选项发送的方法。 但我得到了一些垃圾数据作为回应。 控制器方法:

@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);
}
实施方法:

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");
    }
}
public void returnmentscheduleinexcel(字符串accountNumber,HttpServletResponse){
试一试{
response.setHeader(“内容处置”、“附件;文件名=“+accountNumber+”.xlsx”);
setHeader(“缓存控制”、“无缓存”);
response.setDateHeader(“上次修改”,System.currentTimeMillis());
setContentType(“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”);
String file=“D://text.xlsx”;
FileOutputStream f=新FileOutputStream(新文件(文件));
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“还款计划明细”);
字符串[]头={“序列号”、“交易名称”、“需求日期”、“交易日期”、“分期付款金额”、“余额金额”、“到期利息”、“到期本金”};
Row headerRow=sheet.createRow(0);
for(字符串标题:标题){
int j=0;
Cell Cell=headerRow.createCell(j++);
cell.setCellValue(表头);
}
loanodesummarywsdto transactionSummary=encoreService.findSummary(accountNumber,true);
if(transactionSummary!=null&&transactionSummary.GetReturnmentSchedule()!=null&&transactionSummary.GetReturnmentSchedule().size()>0){
List ReturnmentSchedules=transactionSummary.GetReturnmentSchedule();
对于(交易汇总至还款计划:还款计划){
int rowNumber=1;
Row valueRow=sheet.createRow(rowNumber++);
int columnNumber=0;
Cell seqNum=valueRow.createCell(columnNumber++);
seqNum.setCellValue(returnmentSchedule.getSequenceNum());
Cell transactionName=valueRow.createCell(columnNumber++);
transactionName.setCellValue(RetrymentSchedule.getTransactionName());
单元格valueDate=valueRow.createCell(columnNumber++);
valueDate.setCellValue(returnmentSchedule.getValueDate());
Cell transactionDate=valueRow.createCell(columnNumber++);
transactionDate.setCellValue(ReturnmentSchedule.getTransactionDate());
单元格数量=valueRow.createCell(columnNumber++);
amount.setCellValue(returnmentSchedule.getAmount1());
Cell amount2=valueRow.createCell(columnNumber++);
amount2.setCellValue(returnmentSchedule.getAmount2());
Cell part1=valueRow.createCell(columnNumber++);
part1.setCellValue(ReturnmentSchedule.getPart1());
CellPart2=valueRow.createCell(columnNumber++);
part2.setCellValue(returnmentSchedule.getPart2());
}
}
练习册。写(f);
workbook.close();
System.out.println(“Excel编写成功…”);
}捕获(例外e){
抛出新程序异常(“给定帐号的encore调用失败”);
}
}
输出:

PK 3��J_rels/.rels���J1���公共关系��� "�u/”�Md}��ff�L��F���E] w@�C���?H��9L�R� ��E�������怒族�F�G2v7�G��P�N|�Bb10��K��(`i8Q)���@�E�乌兰巴托�:E�1U흁�wkP�=��yү��'汩�:xK��X�:o��s�('ҏ^�^��|�8.����.���:4EGn�日本脑炎�T��.N8Y��7.����@�?�?����P�主键�A.��� U PK 3��J[内容类型].xml�SMO1���6.��M��1.���G%@mgن~����cDH0��T�޼�:免疫球蛋白㕳�R6�7l����6~ְ��s} ˪��碘化钾����������:B�H��U��N��:p2��҆�$R�f“J5�3׃��P�#x��H����\X�6Ho���%�����;��v�'�='w&�+"��即���8.�连续波����+�'
����(�A-�p(�T�F�Ȅ/ґ� ��,H�����安永�'Z��8.�R� �幓 �&zXc���E8k\�=�(z)�[�ʝ4~��生长激素���LP我认为这不是
应用程序/xls
,而是

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
如图所示

编辑
内容处置
标题的语法不正确:文件名应使用双引号

response.setHeader("Content-Disposition","attachment; filename=\""+ accountNumber+ ".xlsx\"");

尝试
application/vnd.ms excel
而不是
application/xls
application/vnd.openxmlformats of icedocument.spreadsheetml.sheet
,如果它是
xslx
,即使在尝试使用application/vnd.openxmlformats-officedocument.spreadsheetml之后仍然存在相同的问题。‌​即使在尝试使用application/vnd.openxmlformats-officedocument.spreadsheetml之后,工作表和文件的类型“sheet-representsmentscheduleinexcel..”仍然存在相同的问题。‌​sheet,文件类型为“sheet-ReturnMnetScheduleinExcel..”使用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet更新了最新代码