Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SXSSF workbook createSheet() fails on linux environments

Was trying to implement an excel export feature on an app using apache POI 3.17.

All was working fine on my local tomcat server and on a windows dev environment. However, the SXSSFWorkbook workbook.createSheet() method fails on a linux tomcat server without throwing any kind of meaningful error (it's just hanging).

Strangely, the same method on the XSSFWorkbook createSheet class works fine. Below are the snippets of code. Has anyone experienced a similar issue before?

    final SXSSFWorkbook workbook = new SXSSFWorkbook();
    workbook.setCompressTempFiles(true);
    SXSSFSheet sheet = workbook.createSheet("Sheet 1"); //this method fails

    final XSSFWorkbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Sheet 1"); // this works fine

Edit

I created a custom TempFileCreationStrategy to ensure tomcat is writing the file to a directory it has full access to. I can see the file has been created but it's hanging as it tries to write any data to the file.

I cannot figure this one out.

Edit2

I've enabled POI logging but it I'm still not getting anything meaningful that I can investigate. On my local server POI logging spits out the following as it begins to write the file:

[20:13:05,005]DEBUG (?:?) - Save core properties part
[20:13:05,005]DEBUG (?:?) - Save package relationships
[20:13:05,005]DEBUG (?:?) - Save content types part
[20:13:05,005]DEBUG (?:?) - Save part 'docProps/app.xml'
[20:13:05,005]DEBUG (?:?) - Save part 'docProps/core.xml'
[20:13:05,005]DEBUG (?:?) - Save part 'xl/sharedStrings.xml'
[20:13:05,005]DEBUG (?:?) - Save part 'xl/styles.xml'
[20:13:05,005]DEBUG (?:?) - Save part 'xl/workbook.xml'
[20:13:05,005]DEBUG (?:?) - Save part 'xl/worksheets/sheet1.xml'

On the Linux box, it's not even getting as far as the first log output. Need to find a way to get more detail about the failure!

Edit3

Is it possible to get more detailed logging beyond the default logging I have enabled below?

System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.SystemOutLogger" );

String tmpDir = System.getProperty("java.io.tmpdir")+File.separator+"excelfiles";
ExcelFileCreationStrategy tfcs = new ExcelFileCreationStrategy();

try {
    tfcs.createTempDirectory(tmpDir);
} catch (IOException e) {
    e.printStackTrace();
    LOG.error(e);
}

TempFile.setTempFileCreationStrategy(tfcs);

final SXSSFWorkbook workbook = new SXSSFWorkbook();
workbook.setCompressTempFiles(true);

LOG.debug("creating work sheet - next line fails");
Sheet sheet = workbook.createSheet(); //hangs here
LOG.debug("It's worked!!!!");
like image 295
shanahobo86 Avatar asked Feb 26 '18 13:02

shanahobo86


People also ask

How do I create a workbook in Apache POI?

Creating CellsWorkbook wb = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook();

What is XSSF and HSSF?

HSSF (Horrible Spreadsheet Format) − It is used to read and write xls format of MS-Excel files. XSSF (XML Spreadsheet Format) − It is used for xlsx file format of MS-Excel.

What is XSSFWorkbook in Java?

XSSFWorkbook. It is a class that is used to represent both high and low level Excel file formats. It belongs to the org.


1 Answers

I have checked with strace what happens under the hood, and the relevant output was:

11924 openat(AT_FDCWD, "/tmp/out.bin", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 48
11924 openat(AT_FDCWD, "/tmp/poifiles/poi-sxssf-template2618545805950425148.xlsx", O_RDWR|O_CREAT|O_EXCL, 0666) = 49
11924 openat(AT_FDCWD, "/tmp/poifiles/poi-sxssf-template2618545805950425148.xlsx", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 49
11924 openat(AT_FDCWD, "/tmp/poifiles/poi-sxssf-template2618545805950425148.xlsx", O_RDONLY) = 49
11924 openat(AT_FDCWD, "/tmp/poifiles/poi-sxssf-sheet-xml261863645047955641.gz", O_RDONLY) = 21

I guess you need to make sure the /tmp/poifiles is writable by your user. However in my case when I make it non-writable, the app won't hang, but instead throws IOException.

Or, if your java.io.tmpdir is not /tmp, I guess you need to make sure your poifiles subdirectory inside java.io.tmpdir is writable.

like image 120
BaRoN Avatar answered Dec 09 '22 20:12

BaRoN