简体   繁体   中英

text wrap when creating .csv file with NSMutableArray

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 .

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: ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM