0

I am creating .csv file with following code

-(NSString *)dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"myfile.csv"];

}
-(void)createfile{

if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
    [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil];
 }
 for (int i=0; i<[arr count]; i++) {
      NSString *_amnt = [[self.reversedArr objectAtIndex:i]  valueForKey:@"amount"];
     writeString = [NSString stringWithFormat:@"%@,%@,%@", [[arr objectAtIndex:i] valueForKey:@"date"], [[arr objectAtIndex:i] valueForKey:@"particular", [NSString stringWithFormat:@"%@\n", _amnt]];


    }
    NSFileHandle *handle;
    handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];
    //say to handle where's the file fo write
    [handle truncateFileAtOffset:[handle seekToEndOfFile]];
    //position handle cursor to the end of file
    [handle writeData:[writeString dataUsingEncoding:NSUTF8StringEncoding]];

}

I am successful to create csv file but I am having a problem when I am opening the .csv file in excel sheet then any row who having more characters , extending its column width according to characters but I need to wrap text with fixed column width , row height should be extend. So what should I do for this task. can any one give suggestion .

3
  • Your problem is excel formatting, but you want to handle that by adding carriage returns to your exported data? Commented Aug 1, 2013 at 6:14
  • I am using this .csv file in uiwebview and then take printout but when characters is more then column is going more and my .csv is cutting in A4 size paper so In need text wrap of more characters in same row. Commented Aug 1, 2013 at 6:25
  • Then you need to add a new line character in your csv for some places where you want to wrap the text. Commented Aug 1, 2013 at 6:34

1 Answer 1

1

Process each string that is going into your format parameters separately. Check the length. If it is greater than your required length, do something to insert carriage returns (like loop and insert characters in a range stringByReplacingCharactersInRange:withString: or NSMutableString insertString:atIndex:).

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

Comments

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.