1
\$\begingroup\$

I have little knowledge about java (I'm just starting). I have issues in my generate excel file code. It takes some time to generate the file. Could you please take a look at it?

private void generateExcelFile(String fileName)
{
    logger.debug("Start creating excel file name " + fileName);
    try
    {
        Map<Integer, Rep_Column> columns = createFileColumnsMap();
        int count = columns.size();
        ScrollData<String[]> scroller = createReportScroller(FILE_NUMBER_OF_READ_ROWS);
        List<String> filters = createParamsValueForFile();
        int filterSize = filters.size();
        Excel excel = Excel.createInstanceExcel2007("Testowy", fileName);
        int startDocumentRow = 5 + filterSize;
        if (filterSize > 0)
            startDocumentRow += 2;
        else
            startDocumentRow++;

        excel.startDocument(startDocumentRow, 1);
        excel.createHeadersLayout(columns);
        excel.writeText(1, 1, report.getName(), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 11, true));

        excel.writeText(2, 1, Utility.getJSFMessage(getResourceBundle(), "fileGeneratedByUser", getUserSession().getUser().getUser().getEmployee().getName() + " " + getUserSession().getUser().getUser().getEmployee().getSurname()), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 10, true));
        excel.writeText(3, 1, Utility.getJSFMessage(getResourceBundle(), "fileGeneratedDate", (Utility.getDateFormat(getLocale()))), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 10, true));
        excel.writeText(4, 1, Utility.getJSFMessage(getResourceBundle(), "fileSetText", findActiveSet()), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 10, true));

        if (!filters.isEmpty())
        {

            excel.writeText(5, 1, Utility.getJSFMessage(getResourceBundle(), "fileFilterText"), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 10, true));

            for (int i = 0; i < filterSize; i++)
            {
                excel.writeText(6 + i, 1, filters.get(i), excel.createStyle(Excel.ALIGN_LEFT, Excel.BLACK, (short) 10, false));

            }
        }

        excel.startDataLayout(scroller, columns);
        excel.writeText(excel.getLastRow() + 2, 1, Utility.getJSFMessage(getResourceBundle(), "fileGeneratedBy"), excel.createStyle(Excel.ALIGN_RIGHT, Excel.ORANGE, (short) 7, true));
        excel.endData();
        excel.startCreateMargin();
        excel.createMargin(1, 1, count);
        excel.createMargin(2, 1, count);
        excel.createMargin(3, 1, count);
        excel.createMargin(4, 1, count);

        if (filterSize > 0)
        {
            excel.createMargin(5, 1, count);

            for (int i = 0; i < filterSize; i++)
            {
                excel.createMargin(6 + i, 1, count);
            }
        }

        excel.createMargin(excel.getLastRow() + 2, 1, count);
        excel.endCreateMargin();
        excel.endSaveDocument();

        this.downloadFilePath = fileName;
        this.downloadFileName = report.getName() + ".xlsx";

        logger.debug("Generated file=" + fileName);
    }
    catch (Exception e)
    {
        logger.error("Error", e);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block. \$\endgroup\$ – 200_success Dec 4 '17 at 21:40
  • 3
    \$\begingroup\$ Also describe what the Excel object is, and what the generated Excel file contains. See How to Ask. \$\endgroup\$ – 200_success Dec 4 '17 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.