附上csv到电子邮件xcode

我有一个工作的csv附件到电子邮件视图。 问题是,当我在iPhone上打开CSV文件,它显示的文件真的很好分成不同的列。 但是,如果我在Excel中打开它,这一切都在一个领域。 我需要两栏如何做到这一点? 试图用逗号分隔领域,但没有工作(显然)。 以下几行是那些必须以某种方式不正确的行,这是我build立我的string写入文件的地方

[csv appendFormat:@"\n\"Total # of tables: %@\",Total # of objects: %d, \n \n", filteredTables,filteredTableRows] [csv appendFormat:@"\n\"%@\",%@,", key, value]; 

这是我的代码(复制,似乎是标准的)写入文件

 BOOL res = [csv writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (!res) { NSLog(@"Error %@ while writing to file %@", [error localizedDescription], fileName); } 

为什么它不按我想要的方式工作,以及如何将string分隔到不同的列中? 谢谢!

对于有兴趣使用MessageUI.framework和MFMailComposeViewControllerDelegate创build和添加csv到电子邮件的其他人,以下是创build新csv的部分,将其保存到文件并将其全部整合到一起。 你知道,如果你是这样的事情

 NSString *emailTitle = @"My Email Title"; NSString *messageBody = @"Email Body"; MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; mc.mailComposeDelegate = self; [mc setSubject:emailTitle]; [mc setMessageBody:messageBody isHTML:NO]; [mc setToRecipients:@[]]; NSMutableString *csv = [NSMutableString stringWithString:@""]; //add your content to the csv [csv appendFormat:@"MY DATA YADA YADA"]; NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* fileName = @"MyCSVFileName.csv"; NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName]; if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) { [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil]; } BOOL res = [[csv dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO]; if (!res) { [[[UIAlertView alloc] initWithTitle:@"Error Creating CSV" message:@"Check your permissions to make sure this app can create files so you may email the app data" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil] show]; }else{ NSLog(@"Data saved! File path = %@", fileName); [mc addAttachmentData:[NSData dataWithContentsOfFile:fileAtPath] mimeType:@"text/csv" fileName:@"MyCSVFileName.csv"]; [self presentViewController:mc animated:YES completion:nil]; } 

解决我自己的问题很简单(如果有人遇到我的post):我用分号replace了“,”

 [csv appendFormat:@"\n%@ ;%@,", key, value]; 

(不需要\“,因为它已经是一个string了),它现在可以和excel一起工作,但是在iPhones / iPad上不能正确显示,因为字段分隔符有一个”,“。

但是我的问题解决了。