Attach csv to email xcode
Asked Answered
P

2

0

I got a working csv attachment to an email view. The problem is, that when I open the csv on the iPhone it displays the file really nice into separate columns. But if I open it in excel it's all in one field. I need two columns how can I do that? Tried to separate fields with commas but that didn't work (obviously). The following lines are the ones that must be incorrect somehow, this is where I build my string to write to file

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

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

And this is my code (copied and seems to be standard) for the writing to file

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

Why is it not working the way I want it to, and how do I get the string to separate into different columns? Thanks!

Proustite answered 11/7, 2014 at 8:28 Comment(0)
P
0

The solution to my own problem was quite simple (if anyone ever happens to come across my post): I replaced the "," by a semicolon so

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

(don't need the \" since it*'s a string already). It works with excel now but does not display correctly on iPhones/iPads anymore because the field separator there is a ",".

But my problem is solved.

Proustite answered 14/7, 2014 at 14:49 Comment(0)
E
2

For anyone else interested in creating and adding a csv to an email using the MessageUI.framework and MFMailComposeViewControllerDelegate, Here are the parts where you create a new csv, save it to file and attach it all in one. You know, if you're in to that sort of thing

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];
}
Ecclesiology answered 5/9, 2014 at 2:0 Comment(0)
P
0

The solution to my own problem was quite simple (if anyone ever happens to come across my post): I replaced the "," by a semicolon so

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

(don't need the \" since it*'s a string already). It works with excel now but does not display correctly on iPhones/iPads anymore because the field separator there is a ",".

But my problem is solved.

Proustite answered 14/7, 2014 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.