Monday, 22 September 2014

Generate CSV file for Export the datas’s

.h File
UIAlertView *Alert;
BOOL mail_status;

.m File:
-(void)Generate_CSV
{
    NSString *csv_values = [NSString stringWithFormat:@“Names”];   

NSString *string_heading =@"Others”;
    csv_values = [NSString stringWithFormat:@"%@, ""%@""", csv_values,string_heading];
    
    for (int i=0; i<[Array count]; i++)
    {
        NSString *str_products = [Array objectAtIndex:i];
        str_products = [str_products stringByReplacingOccurrencesOfString:@" " withString:@""];
        csv_values = [NSString stringWithFormat:@"%@, ""\n""""%@""", csv_values,str_products];
    }
    
    if (mail_status)
    {
        NSData *csv_data = [csv_values dataUsingEncoding:NSUTF8StringEncoding];
        
        if ( [MFMailComposeViewController canSendMail] )
        {
            MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
            mailComposer.mailComposeDelegate = self;
            [mailComposer addAttachmentData:csv_data mimeType:@"text/csv" fileName:@"results.csv"];
            [mailComposer setSubject:@"Results CSV File”];
            
            [self presentViewController:mailComposer animated:YES completion:NULL];
        }
        else
        {
              UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"No Mail Accounts" message:@"Please set up a Mail account in order to send email." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
               [alert show];
        }
    }
    
    else
    {
        NSString *file = @"/results.csv";
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];
        NSString *filePaths = [docDirectory stringByAppendingString:file];
        [csv_values writeToFile:filePaths atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        [self open_csv];
    }

}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sending Failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;

        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}


-(void)open_csv
{
    csv_view.hidden = NO;
    csv_view.layer.masksToBounds = YES;
    
    //Show the Excel in WebView
    NSString *file = @"/results.csv";
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [arrayPaths objectAtIndex:0];
    NSString *filePath = [docDirectory stringByAppendingString:file];
    if (filePath)
    {
        NSURL *url=[NSURL fileURLWithPath:filePath];
        NSURLRequest *req=[NSURLRequest requestWithURL:url];
        [csv_content loadRequest:req]; //csv_content -- WebView
    }
}


-(IBAction)csv_close:(id)sender
{
    csv_view.hidden = YES;
}

No comments: