I want to write into HSSFWorkBook or XSSFWorkBook in Multhreading environment. Each thread will do the modification in same or different sheet.
try {
String filePath="C:/Test.xlsx";
FileInputStream fileInputStream = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fileInputStream);
FileOutputStream fos = new FileOutputStream(filePath);
workbook.write(fos);
fos.close();
fileInputStream.close();
} catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
While testing this code in LoadTest with MultiThreading in SOAPUI. I got the exception in line:
Workbook workbook = new XSSFWorkbook(fileInputStream);
The exception is as follows:
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
Be aware: Apache POI explicitly does not support multi-threading access to the same workbook-object! This is because there are structures that are handled on a workbook-level, e.g. Styles, Comments, ...
You will run into obscure errors and corrupted documents if you try to do this naively.
The only guarantee that it makes is that separate workbooks in different threads will work fine, i.e. there is no thread-unsafe global state kept anywhere.
The only way that should work would be to synchronize every access to the workbook via a synchronized block:
synchronized (workbook) {
... access the sheet and the contents
}
Read-only access might work, but again Apache POI does not make guarantees that concurrent read-access to the same workbook will work.
Update: There is now a corresponding FAQ entry stating this as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With