我正在创建一个服务调用来从我们的数据库中获取数据并将其下载为Excel文件。当我调用该服务时,我得到一个要下载的文件,但是当我打开它时它是空的!我没有在日志中看到任何错误,事实上,我的记录器显示一切都很好。
Excel Builder
public class PromotionExportExcelBuilder extends AbstractExcelView {
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<PromotionRequestDTO> promoCodes = (List<PromotionRequestDTO>) model.get("promoCodes");
if (CollectionUtils.isNotEmpty(promoCodes)) {
ServletOutputStream output = response.getOutputStream();
// create style for header cells
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("Arial");
style.setFillForegroundColor(HSSFColor.BLUE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.WHITE.index);
style.setFont(font);
Integer totalPromos = promoCodes.size();
logger.debug(totalPromos + " promo codes are being exported");
Integer numberOfSheets = 1;
// Only want a set number of codes per sheet
if (totalPromos > PromotionMicroServiceBusinessConstants.PROMO_RECORDS_PER_SHEET) {
numberOfSheets = (totalPromos / PromotionMicroServiceBusinessConstants.PROMO_RECORDS_PER_SHEET ) + 1;
}
logger.debug(numberOfSheets + " sheets will be needed");
for (int sheetNumber = 1; sheetNumber <= numberOfSheets; sheetNumber++) {
logger.debug("Making sheet number " + sheetNumber);
HSSFSheet sheet = workbook.createSheet("Promotion Codes Sheet " + sheetNumber);
sheet.setDefaultColumnWidth(30);
HSSFRow header = sheet.createRow(0);
header.createCell(0).setCellValue("CAMPAIGN");
header.getCell(0).setCellStyle(style);
header.createCell(1).setCellValue("PROMO CODE");
header.getCell(1).setCellStyle(style);
int rowCount = 1;
/*
CODE TO FILL OTHER ROWS. CURRENTLY COMMENTED OUT FOR TESTING
*/
logger.debug("Sheet number " + sheetNumber + " created");
}
workbook.write(output); // Actually write the workbook.
}
}
}
然后,当我查看日志时,我看到了:
28-Jun-2017 17:55:08.122 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 10904 ms
17:55:15.031 [http-nio-8080-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'promocode' processing GET request for [/promocode-0.0.1-SNAPSHOT/rest/exportPromotionCodes]
17:55:15.038 [http-nio-8080-exec-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /exportPromotionCodes
17:55:15.041 [http-nio-8080-exec-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView com.otc.promotion.controller.PromotionController.exportPromotionCodes(java.lang.String,java.lang.String)]
17:55:15.041 [http-nio-8080-exec-2] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'promotionController'
17:55:15.046 [http-nio-8080-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/promocode-0.0.1-SNAPSHOT/rest/exportPromotionCodes] is: -1
17:55:15.069 [http-nio-8080-exec-2] INFO c.o.p.controller.PromotionController - Request to Export promotion codes for campaign[17] and batch [null]
17:55:15.293 [http-nio-8080-exec-2] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'promotionExportView'
17:55:15.294 [http-nio-8080-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [com.otc.promotion.views.PromotionExportExcelBuilder: name 'promotionExportView'] in DispatcherServlet with name 'promocode'
17:55:15.414 [http-nio-8080-exec-2] DEBUG c.o.p.v.PromotionExportExcelBuilder - Created Excel Workbook from scratch
17:55:15.425 [http-nio-8080-exec-2] DEBUG c.o.p.v.PromotionExportExcelBuilder - 30 promo codes are being exported
17:55:15.426 [http-nio-8080-exec-2] DEBUG c.o.p.v.PromotionExportExcelBuilder - 1 sheets will be needed
17:55:15.426 [http-nio-8080-exec-2] DEBUG c.o.p.v.PromotionExportExcelBuilder - Making sheet number 1
17:55:15.512 [http-nio-8080-exec-2] DEBUG c.o.p.v.PromotionExportExcelBuilder - Sheet number 1 created
17:55:15.574 [http-nio-8080-exec-2] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
当我打开下载的文件时,我看到了:
答案 0 :(得分:0)
所以我想出了这个问题。这根本不是REST调用的问题。我的Excel搞砸了,打开了一个空的&#34;每当我点击Excel文件时应用程序。如果我从Microsoft Excel打开文件,我会看到数据。