------- мой сервисный код --------
public File downloadExcelByDraw(Integer qeiMasterId, String fileName, NMTCService nmtcService) throws Exception {
this.nmtcService = nmtcService;
File file = null;
Qei qeiDetails = nmtcService.getQeiDetail(qeiMasterId);
log.info("Inside downloadExcelByDraw: " + qeiMasterId);
file = new File(fileName);
if(!file.exists()) {
boolean fileCreated = file.createNewFile();
}
try (FileInputStream fis = new FileInputStream(file);
//here it throws empty file exception having zero bytes.
Workbook workbook = new XSSFWorkbook(fis);) {
log.info("Inside downloadExcelByDraw: Excel Template Name:: " + ExcelConstants.TEMPLATE_EXCEL.getName());
Sheet templateSheet = workbook.getSheet(ExcelConstants.TEMPLATE_SHEET_NAME.getName());
fis.close();
if (qeiDetails != null) {
String sheetName = ExcelConstants.SHEET_NAME_PREFIX.getName();
try {
super.cloneSheet(templateSheet, sheetName);
} catch (Exception e) {
log.error("Error in downloadExcelByDraw() :" + e.getStackTrace());
}
}
workbook.removeSheetAt(workbook.getSheetIndex(templateSheet));
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
log.info("sheet index:" + i + ", sheet name: " + sheet.getSheetName());
ExcelDataProvider excelDataProvider = new ExcelDataProvider(qeiDetails);
fillNamedRanges(ExcelConstants.getQeiMappings(), excelDataProvider, sheet);
}
// file creation changed for file creation
// log.info("Inside downloadExcelByDraw:filePath:: " + filePath);
FileOutputStream fos = new FileOutputStream(file, true);
workbook.write(fos);
workbook.close();
log.info("Inside downloadExcelByDraw:fileCreated:: " + file);
fos.flush();
fos.close();
} catch (Exception e) {
log.error("Error in downloadExcelByDraw() :" + e.getMessage());
}
return file;
}