Я использую Apache POI 4.1.0 для создания файла Excel из кода java в определенном месте. Когда я запускаю его на локальном компьютере, файл генерируется надлежащим образом, но когда я создаю WAR и загружаю на сервер, чтобы выполнить ту же операцию создания файла Excel, выдается исключение, как показано ниже:
2020-02-28 12:19:58,309 ERROR (com.credentek.recon.threadReportGeneration.ProductLevelReportsGeneration:1454) - Exception :
org.apache.poi.ooxml.POIXMLException: OOXML file structure broken/invalid - core document '/xl/workbook.xml' not found.
at org.apache.poi.ooxml.POIXMLDocumentPart.getPartFromOPCPackage(POIXMLDocumentPart.java:743)
at org.apache.poi.ooxml.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:148)
at org.apache.poi.ooxml.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:138)
at org.apache.poi.ooxml.POIXMLDocument.<init>(POIXMLDocument.java:61)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:276)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:88)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:339)
at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:314)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:232)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:198)
at com.credentek.recon.threadReportGeneration.ProductLevelReportsGeneration.PrepareBRSStatementSheet(ProductLevelReportsGeneration.java:1327)
at com.credentek.recon.threadReportGeneration.ProductLevelReportsGeneration.GenerateProductLevelReports(ProductLevelReportsGeneration.java:213)
at com.credentek.recon.main.ReportGenerationSchedular.ExecuteReport(ReportGenerationSchedular.java:126)
at com.credentek.recon.main.ReportGenerationSchedular.ReportGenerationSheduler(ReportGenerationSchedular.java:69)
at com.credentek.recon.main.Main$1.run(Main.java:85)
at java.lang.Thread.run(Thread.java:748)
Мой код для создания файла Excel, как показано ниже
private void PrepareBRSStatementSheet(String p_fileName, String p_sheetName, String p_productName, String p_asOnDate, String p_preparedBy,
String p_checkedBy, String p_reviewedBy, String p_desclaimer) throws Exception {
log.info(p_sheetName + " Sheet writting started");
File l_file = null;
OutputStream l_fos = null;
XSSFWorkbook l_workbook = null;
XSSFSheet l_sheet = null;
XSSFRow l_row = null;
int l_rowCount = 0;
double l_crByBankNotByUs = 0.0;
double l_crByUsNotByBank = 0.0;
double l_drByBankNotByUs = 0.0;
double l_drByUsNotByBank = 0.0;
double l_difference = 0.0;
double l_closingBookBalanceTotal = 0.0;
double l_closingBankBalanceTotal = 0.0;
try {
// ---------Fetching values to print in report from JSON object
l_crByUsNotByBank = Math.abs(Double.parseDouble(brsStatementJson.opt("j_crByUsNotByBank").toString()));
l_drByUsNotByBank = Math.abs(Double.parseDouble(brsStatementJson.opt("j_drByUsNotByBank").toString()));
l_crByBankNotByUs = Math.abs(Double.parseDouble(brsStatementJson.opt("j_crByBankNotByUs").toString()));
l_drByBankNotByUs = Math.abs(Double.parseDouble(brsStatementJson.opt("j_drByBankNotByUs").toString()));
l_closingBookBalanceTotal = Double.parseDouble(brsStatementJson.opt("j_closingBookBalanceTotal").toString()) * -1;
l_closingBankBalanceTotal = Double.parseDouble(brsStatementJson.opt("j_closingBankBalanceTotal").toString());
// ---------Calculating diff value according to formula
l_difference = l_closingBankBalanceTotal - l_crByBankNotByUs - l_crByUsNotByBank + l_drByBankNotByUs + l_drByUsNotByBank - l_closingBookBalanceTotal;
log.info(l_difference +"="+ l_closingBankBalanceTotal +"-"+ l_crByBankNotByUs +"-"+ l_crByUsNotByBank +"+"+ l_drByBankNotByUs +"+"+ l_drByUsNotByBank +"-"+ l_closingBookBalanceTotal);
l_file = new File(p_fileName);
if (l_file.exists()) {
l_workbook = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(l_file));
} else {
l_workbook = new XSSFWorkbook();
}
//--------Diff Cell Style creation started
Font l_boldFont = l_workbook.createFont();
l_boldFont.setBold(true);
l_boldFont.setFontHeightInPoints((short) 10);
XSSFCellStyle l_disclaimerStyle = l_workbook.createCellStyle();
l_disclaimerStyle.getWrapText();
XSSFCellStyle l_leftDataStyle = l_workbook.createCellStyle();
l_leftDataStyle.setFont(l_boldFont);
l_leftDataStyle.setBorderTop(BorderStyle.THIN);
l_leftDataStyle.setBorderRight(BorderStyle.THIN);
l_leftDataStyle.setBorderBottom(BorderStyle.THIN);
l_leftDataStyle.setBorderLeft(BorderStyle.THIN);
XSSFCellStyle l_rightDataStyle = l_workbook.createCellStyle();
l_rightDataStyle.setFont(l_boldFont);
l_rightDataStyle.setBorderTop(BorderStyle.THIN);
l_rightDataStyle.setBorderRight(BorderStyle.THIN);
l_rightDataStyle.setBorderBottom(BorderStyle.THIN);
l_rightDataStyle.setBorderLeft(BorderStyle.THIN);
XSSFCellStyle l_topHeaderStyle = l_workbook.createCellStyle();
l_topHeaderStyle.setFont(l_boldFont);
l_topHeaderStyle.setAlignment(HorizontalAlignment.CENTER);
l_topHeaderStyle.setBorderTop(BorderStyle.THIN);
l_topHeaderStyle.setBorderRight(BorderStyle.THIN);
l_topHeaderStyle.setBorderBottom(BorderStyle.THIN);
l_topHeaderStyle.setBorderLeft(BorderStyle.THIN);
byte[] l_middleHeaderColor = { (byte) 183, (byte) 222, (byte) 232 };
XSSFCellStyle l_middleHeaderStyle = l_workbook.createCellStyle();
l_middleHeaderStyle.setFillForegroundColor(new XSSFColor(l_middleHeaderColor, new DefaultIndexedColorMap()));
l_middleHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
l_middleHeaderStyle.setFont(l_boldFont);
l_middleHeaderStyle.setAlignment(HorizontalAlignment.CENTER);
l_middleHeaderStyle.setBorderTop(BorderStyle.THIN);
l_middleHeaderStyle.setBorderRight(BorderStyle.THIN);
l_middleHeaderStyle.setBorderBottom(BorderStyle.THIN);
l_middleHeaderStyle.setBorderLeft(BorderStyle.THIN);
byte[] l_lowerHeaderColor = { (byte) 252, (byte) 213, (byte) 181 };
XSSFCellStyle l_lowerHeaderStyle = l_workbook.createCellStyle();
l_lowerHeaderStyle.setFillForegroundColor(new XSSFColor(l_lowerHeaderColor, new DefaultIndexedColorMap()));
l_lowerHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
l_lowerHeaderStyle.setFont(l_boldFont);
l_lowerHeaderStyle.setAlignment(HorizontalAlignment.CENTER);
l_lowerHeaderStyle.setBorderTop(BorderStyle.THIN);
l_lowerHeaderStyle.setBorderRight(BorderStyle.THIN);
l_lowerHeaderStyle.setBorderBottom(BorderStyle.THIN);
l_lowerHeaderStyle.setBorderLeft(BorderStyle.THIN);
// --------Diff Cell Style creation ended
// ---------Report data plotting started
l_sheet = l_workbook.createSheet(p_sheetName);
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, l_topHeaderStyle, "L&T Finance Limited - " + p_productName + " (No of Accounts: " + (totalAccountCount) + ")");
l_sheet.addMergedRegion(new CellRangeAddress(l_rowCount, l_rowCount, 1, 2));
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, l_middleHeaderStyle, "Bank Reconciliation Statement as on " + Utils.convertDateFormats(p_asOnDate, "yyyy-MM-dd", "dd MMM yyyy"));
l_sheet.addMergedRegion(new CellRangeAddress(l_rowCount, l_rowCount, 1, 2));
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, l_lowerHeaderStyle, "Details");
AddCell(l_row, 2, l_lowerHeaderStyle, "Amount (Rs)");
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "A. Closing Balance as per Bank Statement");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_closingBankBalanceTotal));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "B. Credit by Bank & Not By Us - Cash Deposit /Cheque Collection");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_crByBankNotByUs));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "C. Credit By Us & Not By Bank - Book Reversal");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_crByUsNotByBank));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "D. Debit by Bank & Not By Us - Cheque Return / Charges");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_drByBankNotByUs));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "E. Debit By Us & Not By Bank - Cheque Deposited");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_drByUsNotByBank));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "F. Closing Balance As per Bank Book");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_closingBookBalanceTotal));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, l_leftDataStyle, "Difference = A - B-C + D+E - F = 0");
AddCell(l_row, 2, l_rightDataStyle, String.format("%.2f", l_difference));
l_row = l_sheet.createRow(l_rowCount += 2);
AddCell(l_row, 1, null, "Prepared By");
AddCell(l_row, 2, null, p_preparedBy);
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, null, "Checked By");
AddCell(l_row, 2, null, p_checkedBy);
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, null, "Reviewed By");
AddCell(l_row, 2, null, p_reviewedBy);
l_row = l_sheet.createRow(++l_rowCount);
AddCell(l_row, 1, null, "Disclaimer");
AddCell(l_row, 2, l_disclaimerStyle, p_desclaimer);
// ---------Report data plotting ended
// ---------Expanding Columns to fit to data.
l_sheet.autoSizeColumn(1, true);
l_sheet.autoSizeColumn(2, true);
l_fos = new FileOutputStream(l_file);
l_workbook.write(l_fos);
l_fos.flush();
} catch (Exception e) {
log.error("Exception : ",e);
} catch (Throwable tw) {
log.error("Throwable : ",tw);
} finally {
try {
if (l_fos != null) {
l_fos.close();
}
} catch (IOException e) {
log.error("IOException : ",e);
}
try {
if (l_workbook != null) {
l_workbook.close();
}
} catch (IOException e) {
log.error("IOException : ",e);
}
}
log.info(p_sheetName + " Sheet writting ended");
}