Thursday, 19 March 2015

Email .csv(excel format):

.h file:

#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>

.m file:

@interface ViewController ()
{    
    NSMutableArray * expansesArray;
    NSString *Expensereport, *emailbody;
    MFMailComposeViewController *mc;
}


-(IBAction)sendmail:(id)sender{
    
    Expensereport = [NSString stringWithFormat:@"Expense report for"];
    emailbody = [NSString stringWithFormat:@"Dear, \n\n Attached to this mail is the expense report for as per ."];
    /* ================= CSV FILE ================*/
    
 NSMutableString *csv;
        csv = [NSMutableString stringWithString:@" , , "];
        
        [csv appendFormat:@"\n\"%@\",%@,\"%@\"",[NSString stringWithFormat:@"NAME"],[NSString stringWithFormat:@"%@ %@",self.sFname,self.sLname],@""];
        
        [csv appendFormat:@"\n\"%@\",%@,\"%@\"",[NSString stringWithFormat:@"AMOUNT"],[NSString stringWithFormat:@"%@ %.2f",BudgetLabel.text,person_amt],@""];
        
        [csv appendFormat:@"\n\"%@\",%@,\"%@\"",[NSString stringWithFormat:@"AVERAGE"],[NSString stringWithFormat:@"%@ %.2f",BudgetLabel.text,(Tot_exp / Tot_per)],@""];
        
       [csv appendFormat:@"\n"];
        
        [csv appendFormat:@"\n\"%@\",%@,\"%@\"",@"Item name",@"Date",@"Amount"];
        
        [csv appendFormat:@"\n"];
               
        NSUInteger count = [expansesArray count];
        // provided all arrays are of the same length
        for (NSUInteger i=0; i<count; i++ ) {
            
            NSDictionary *tmpDict = [expansesArray objectAtIndex:i];
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *date1 = [dateFormatter dateFromString: [tmpDict objectForKey:datetime_exp]];
            dateFormatter = [[NSDateFormatter alloc] init] ;
            [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
            NSString *DateString = [dateFormatter stringFromDate:date1];
            
            [csv appendFormat:@"\n\"%@\",%@,\"%f\"",
             [tmpDict objectForKey:item_name],
             DateString,
             [[tmpDict objectForKey:expenses_amount] floatValue]
             ];
        }
        
        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
        NSString *docPath = [sysPaths objectAtIndex:0];
        NSString *surveys = [docPath stringByAppendingPathComponent:@"expense.csv"];
        NSError *error;
        BOOL res = [csv writeToFile:surveys atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (!res) {
            NSLog(@"Error %@ while writing to file %@", [error localizedDescription], surveys );
        }        
        
        mc = [[MFMailComposeViewController alloc] init];
        if ([MFMailComposeViewController canSendMail]) {
            mc.mailComposeDelegate = self;
            [mc setSubject:Expensereport];
            [mc setMessageBody:emailbody isHTML:NO];
            
            [mc addAttachmentData:[NSData dataWithContentsOfFile:surveys]
                         mimeType:@"text/csv"
                         fileName:@"expense.csv"];
            // Present mail view controller on screen
            [self presentViewController:mc animated:YES completion:NULL];
        }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

No comments:

Post a Comment