ios - How to write array data into excel file (CSV) in objective c -
i trying write array data excel (actually csv, opened in excel). used following code that:
nsmutablearray *list; list = [[nsmutablearray alloc] init]; nsstring *string = [list componentsjoinedbystring:@","]; nsdata *data = [string datausingencoding:nsutf8stringencoding]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *appfile = [documentsdirectory stringbyappendingpathcomponent:@"yourfilename.csv"]; [data writetofile:appfile atomically:yes];
it works fine, problem have 20 objects in 'list' array , 20 objects written in side side cells. want write first 4 objects in 1 line , move new line , again write next 4 objects in line till objects in list array completed.
can me issue?
nsmutablearray *list = ... nsmutablestring *buffer = [nsmutablestring string]; (nsuinteger = 0; < list.count; i++) { nsstring *value = list[i]; if (i > 0) { if (i % 4 == 0) { //after every 4th value buffer.append("\n"); //end line } else { buffer.append(","); } } buffer.append(value); //if values contain spaces, should add quotes around values... //buffer.appendformat(@"\"%@\"", value); } nsdata *data = [buffer datausingencoding:nsutf8stringencoding]; ...
Comments
Post a Comment