Exce And Apache POI and Java Acton

0
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.  
asked
3 answers
1

Hi Lalit , 

You can use below snippet code 

XSSFWorkbook workbook = new XSSFWorkbook ();

// In between lot of code exist

ByteArrayOutputStream bytearraystream = new ByteArrayOutputStream();

workbook.write(bytearraystream);

// Convert to ByteArray

byte[] barray = bytearraystream.toByteArray();

InputStream is = new ByteArrayInputStream(barray);

workbook.close();

// Store the input stream to file document object.

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

Yourfiledocumentobject.setHasContents(true);

answered
1

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);

https://apidocs.rnd.mendix.com/8/runtime/com/mendix/core/Core.html#storeFileDocumentContent(com.mendix.systemwideinterfaces.core.IContext,com.mendix.systemwideinterfaces.core.IMendixObject,java.io.InputStream)

I hope this helps.

answered
0

Hi there,

use the Core method

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

to write to a mendix FileDocument.
regards

answered