0

In my project I am using apache-poi to let the user download excel representation of some data stored in database (kind of a report). When testing locally everything works fine. When running on server, excel file contains only headers and no data at all. At the same time some reports return correctly excel files with data in them, but some of them return empty files with header row only.

In the project report has two ways of data representation: web-based and excel downloadable file. Web-based view works correctly and lets me know that report does return data. So it is strange that excel file is empty.

The problem is that logs are empty and response code is 200 when running in server - so I don't get any error messages that could help me to solve the problem. On the other hand running the same code locally returns me an excel with data in it - so again, I don't get any errors and can't debug.

Below is the class I am using to create excel file.

ExcelUtils.java

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import team.alabs.hcms.model.Header;
import team.alabs.hcms.model.Report;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtils {

    public static Workbook createWorkbook(Report report, List<Map<String, Object>> reportData, List<Header> headers) {
        Workbook reportBook = new XSSFWorkbook();
        String empty = new String("");
        Sheet sheet = reportBook.createSheet("report");
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.size(); i++) {
            headerRow.createCell(i).setCellValue(headers.get(i).getTitle());
        }
        for (int i = 0; i < reportData.size(); i++) {
            Row row = sheet.createRow(i + 1);
            HashMap<String, Object> tmprow = (HashMap<String, Object>) reportData.get(i);
            for (int j = 0; j < headers.size(); j++) {
                Object value = tmprow.get(headers.get(j).getCode()) != null ? tmprow.get(headers.get(j).getCode()) : empty;
                row.createCell(j).setCellValue(value.toString());
            }
        }
        return reportBook;

    }

    public static byte[] getBytes(Workbook workbook) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bos.close();
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    }

}

I expect that running project on server will return excel with data inside.

  • 2
    If sometimes it works fine, then the other times you simply pass in no report data as they seem to always have a header – XtremeBaumer May 23 '19 at 06:02
  • You could add your own logging statements. For example add `logger.debug(String.format("create report for %s data elements", reportData.size()")` at the beginning of your method `createWorkbook`. Then you need to set log level for your class to `DEBUG` of course. If there is no data you need to look somewhere else. – cmoetzing May 23 '19 at 06:03

1 Answers1

0

I had a similar issue where excel file was getting created in my local system with whole data but when it was running in server it is creating a excel with 0 (zero) bytes size. This is because the execution never reaches -

workbook.write(bos); // From your code above

And the reason is - In my case it was in multithreaded environment (using ThreadPoolTaskExecutor). So this is what was the issue - Async thread was getting terminated with uncaughtexception which get caught in FutureTask but never been thrown back so the application not aware of any failure and that thread stops(ends) there. There are ways we can enable handler for such kind of exceptions. Other way is -

Future<?> future = taskExecutor.submit(runnableTask);
future.get(); // This will throw an exception if the thread has thrown an uncaughtexception.

Reference link - Java FutureTask completion check

Based on the outcome, one can easily find the root cause.

Hope this helps!