Exce And Apache POI and Java Acton

So, I want to generate excel file in java action using apache poi library. For this i am passing a file document where the result of writing excel text is to be stored. In java action i have done writing. and after writing there is method XSSFWorkbook.write(OutputStream)  that writes all changes we have done into given output stream. The problem is how to get output stream from file document and/or store changes in file document. How to get FileOutputStream from mendix file document? Please help.   Code to write excel into fileoutputstream from file path. // Now it will write the output to a file FileOutputStream fileOut = new FileOutputStream(FILE_NAME); wb.write(fileOut); But we don’t have file we have mendix  File Document.  
3 answers

Hi Lalit , 

You can use below snippet code 

XSSFWorkbook workbook = new XSSFWorkbook ();

// In between lot of code exist

ByteArrayOutputStream bytearraystream = new ByteArrayOutputStream();


// Convert to ByteArray

byte[] barray = bytearraystream.toByteArray();

InputStream is = new ByteArrayInputStream(barray);


// Store the input stream to file document object.

Core.storeFileDocumentContent(getContext(), Yourfiledocumentobject, is);



As Fabian mentioned, you need to the storeFileDocumentContent method to save to a Mendix FileDocument. As you have an OutputStream, you need to convert this to an InputStream before you can use it with this method. You also need to create a FileDocument object for the method to use before you can store your data.

Assuming you have created your FileDocument and passed it into your Java Action in a variable called yourFileDocument, the following should work. It will convert your fileOut OutputStream into a ByteArrayInputStream that you can use with soreFileDocumentContent.

ByteArrayInputStream inStream = new ByteArrayInputStream(fileOut.toByteArray());
Core.storeFileDocumentContent​(this.getContext(), yourFileDocument, inStream);


I hope this helps.


Hi there,

use the Core method

public static void storeFileDocumentContent​(IContext context, IMendixObject fileDocument, java.io.InputStream inputStream)

to write to a mendix FileDocument.