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.