Free Support Forum - aspose.com

Convert to CSV in memory instead of save to disk? Or create an md5 hash?

Hi,


Is there any way to save a CSV to a variable in memory instead of a file or Java FileStream?

We’re using aspose.cells for Java under coldfusion to extract data from an excel file and convert to CSV.

Before saving to disk we would like to create an MD5 hash of the resulting CSV file in memory to compare with existing records before saving.

Thanks!
Jeff

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Yes, it is quite possible to save your output stream into a memory stream. Aspose.Cells for Java saves your file in Stream and this Stream could be a file stream or memory stream as per user needs.

For example, the following code creates a workbook object, you can create it from your source/template file or you can create it from scratch with its default constructor, then you do whatever processing you like.

Finally, it saves the workbook object int csv format inside a memory using ByteArrayOutputStream object.

I have fully commented the code and you will not have any trouble understanding it. You can then modify this code as per your needs.

Java


String filePath = “F:\Shak-Data-RW\Downloads\test.xlsx”;


//Create your workbook from any source xls/xlsx file

//You can also create empty/default workbook

Workbook workbook = new Workbook(filePath);


//Do your processing whatever you like


//Now save your workbook into csv format in memory

//We will will use ByteArrayOutputStream because everything will be stored

//in memory

ByteArrayOutputStream bout = new ByteArrayOutputStream();

workbook.save(bout, SaveFormat.CSV);


//Now you can save it on disk to check if the ByteArrayOutputStream

//contains your proper output file data

byte[] outputFileData = bout.toByteArray();


FileOutputStream fout = new FileOutputStream(filePath + “.out.csv”);

fout.write(outputFileData);

fout.close();