我在电子邮件视图中收到了一个有效的 csv 附件。问题是,当我在 iPhone 上打开 csv 时,它会将文件很好地显示在单独的列中。但如果我用 Excel 打开它,它就全部在一个字段中。我需要两列,我该怎么做?尝试用逗号分隔字段,但这不起作用(显然)。以下几行肯定是不正确的,这是我构建字符串以写入文件的地方
[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);
}
为什么它没有按照我想要的方式工作,如何将字符串分成不同的列?谢谢!
对于有兴趣使用 MessageUI.framework 和 MFMailComposeViewControllerDelegate 创建 csv 并将其添加到电子邮件的其他人,以下是您创建新 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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)