Part i -
var
csv = require(
'exportCsvData'
);
var
win = Ti.UI.createWindow({
backgroundColor:
'#ccc'
,
title:
'CSV Import Module'
})
var
createCsv = Titanium.UI.createButton({
title:
'CSV'
,
top:
'140'
,
left:
'110'
,
height:
'40'
,
width:
'115'
,
color:
'black'
});
win.add(createCsv);
createCsv.addEventListener(
'click'
,
function
(e){
var
input = [
[
" data 0"
,
" place 1"
,
" address 2"
,
" name 3"
],
[
" data 0"
,
" place 1"
,
"
address
2"
,
"
name
3"
],
[
" data 0"
,
" place 1"
,
"
address
2"
,
"
name
3"
],
[
" data 0"
,
" place 1"
,
"
address
2"
,
"
name
3"
]
];
var
op = csv.exportCsvData(input);
alert(
"Output file path = "
+ op);
});
win.open();
// Create on exportCsvData.js file
Part ii -
exports.exportCsvData =
function
(input)
{
var
rowTxt =
""
;
for
(
var
i=0;i < input.length; i++){
// row iteration
for
(
var
j = 0; j < input[i].length; j++){
// column iteration
rowTxt +=
'"'
+ input[i][j] +
'"'
;
if
(j < (input[i].length-1))
{
rowTxt +=
','
;
}
}
rowTxt +=
'\n'
;
// adding new line at end of row
}
// creating output file in application data directory
var
outputFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,
'output.csv'
);
// writing data in output file
outputFile.write(rowTxt);
if
(outputFile.exists){
alert(
"CSV generated!!!"
);
}
// return output file path
return
outputFile.nativePath;
}
No comments:
Post a Comment