gpt4 book ai didi

ios - 将 csv 附加到电子邮件 xcode

转载 作者:行者123 更新时间:2023-11-28 22:02:51 24 4
gpt4 key购买 nike

我收到了电子邮件 View 的有效 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];
}

关于ios - 将 csv 附加到电子邮件 xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24693305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com