-2
//NSString *csvString = @"S.No,Task,Date,Time";
//NSArray *csvArray=[csvString componentsSeparatedByString:@","];
// Create .csv file and save in Documents Directory.

NSArray *csvArray =[[NSArray alloc]initWithObjects:@"SNo",@"Task",@"Date",@"Time",nil]; //create instance of NSFileManager // NSFileManager *fileManager = [NSFileManager defaultManager];

//create an array and store result of our search for the documents directory in it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//create NSString object, that holds our exact path to the documents directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Document Dir: %@",documentsDirectory);

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv", @"userdata"]]; //add our file to the path

// [fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; //finally save the path (file)

CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:fullPath atomic:NO];
NSInteger numberOfColumns = 4;
for (NSInteger currentIndex = 0; currentIndex < [csvArray count]; currentIndex++) {
    id field = [csvArray objectAtIndex:currentIndex];
    [csvWriter writeField:field];
    if ((currentIndex % numberOfColumns) == (numberOfColumns - 1)) {
        [csvWriter writeLine];
    }
}
[csvWriter release];
2
  • 2
    Please try to include some non-code text in your question. What is the problem, what results are you currently getting, what results do you want? Commented Jun 16, 2012 at 10:43
  • view should be in csv file .... |SNo.|Date|Time| <--row Commented Jun 16, 2012 at 11:37

1 Answer 1

0

CSV file basically use for making a back up of iphone contacts and it is an Excel file why do you want to make CSV file and if you want, then you have to make an array according to your CSV and for this you have to do study.... well here is code for writing a text in CSV file and save it to in documents directory....

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *FileName = [NSString stringWithFormat:@"%@/File.csv", documentsDirectory];
NSString *Content = [[NSString alloc] initWithFormat:@"%@", stringForCsv];
//stringForCsv is your string which you want to write in file.
[Content writeToFile:FileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

Thank You!

Sign up to request clarification or add additional context in comments.

2 Comments

when i create csv all values from array comes in one column instead i want these values seperated into columns.... ..my output comes in this format ... |SRNo Task Date Time|(all values in one column) but i want values in following format |SRNo|Task|Date|Time|(each value in next column).... i want to achieve it programatically not by clicking on seprated by property of csv file editor;
i have not done it yet .... well you can see stackoverflow.com/questions/1159576/… link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.