convert file to csv to open in excel

343 views
Skip to first unread message

bestfri...@gmail.com

unread,
Sep 23, 2013, 1:39:04 PM9/23/13
to suppor...@runmyprocess.com
Hi

I was trying to change type of the file created during the process to csv format ,so that it can be opened in excel format.

Please find the screenshots in the attachments for reference.

In process, i created an output variable as below

${update_file(file_id,"whereusedlist.csv","application/csv")}

And the file is getting attached to the file upload widget but it is not displayed in csv readable format when we try to open in excel.

Please find the attachment for the format.

Could you please advise how to fix this issue?

file_process.PNG
upload_widget.PNG
file_data.PNG
whereusedlist (3).csv

Sabine El Rassy

unread,
Sep 23, 2013, 5:07:14 PM9/23/13
to RunMyProcess Support Forum
Hello Nagasai,

You're trying to give the Excel file an array of json as an input so it's normal that it's not well formatted. You need to write a function that converts the array of json to another format of data that the excel file can understand.

Here's a JS code example that allows you to convert a json to csv accepted format, all you have to do is to write the function using freemarker.

function JSON2CSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';
    var line = '';
    var head = array[0];
    for (var index in array[0]) {
        line += index + ',';
    }
    line = line.slice(0, -1);
    str += line + '\r\n';
    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            line += array[i][index] + ',';
        }

        line = line.slice(0, -1);
        str += line + '\r\n';
    }
    return str;
}
var array=[{"Id":1,"UserName":"Sam Smith"},
{"Id":2,"UserName":"Fred Frankly"},
{"Id":1,"UserName":"Zachary Zupers"}];
alert(JSON2CSV(array));
/*The output will look like 
* Id, UserName
* 1,Sam Smith
* 2,Fred Frankly
* 1,Zachary Zupers
*/

Regards,
Reply all
Reply to author
Forward
0 new messages