1) I’m trying to create CSV file using NSMutableArray(array of array) which I want to send as a mail attachment using MFMailComposeViewController.Following is code i have written for it –
//Export Data to excel
-(NSString*)ExportToExcelClicked
{
dataToSend = @"";
@try {
for(int cntHeader = 0;cntHeader< [NamesArray count];cntHeader++)
{
dataToSend = [NSString stringWithFormat:@"%@%@\n",dataToSend,[NamesArray objectAtIndex:cntHeader]];
dataToSend = [NSString stringWithFormat:@"\nAccount Number,Engine Number,Chasssis Number,Registration,Vehicle Type,Vehicle Model,Vehicle Make\n"];
for(int cntData = 0;cntData< [appDelegate.arrEmailRepossessionDetail count];cntData++)
{
NSMutableArray *thearr = [appDelegate.arrEmailRepossessionDetail objectAtIndex:cntData];
NSString *strVehicleDetail = @"";
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:0]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:1]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:2]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:3]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:4]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:5]];
strVehicleDetail = [NSString stringWithFormat:@"%@%@,",strVehicleDetail,[thearr objectAtIndex:6]];
dataToSend = [NSString stringWithFormat:@"%@%@\n",dataToSend,strVehicleDetail];
}
dataToSend = [NSString stringWithFormat:@"%@\n\n",dataToSend];
return dataToSend;
}
}
@catch (NSException *exception) {
NSLog(@"NSException: %@", exception.reason);
}
}
Code for mail attachment
NSString *strCSVData = [self ExportToExcelClicked];
if ([strCSVData isEqualToString:@""] || strCSVData.length > 0)
{
NSData *dataObj = [strCSVData dataUsingEncoding:NSUTF8StringEncoding];
}
The problem is csv file is getting properly created for around 800 to 900 records,but beyond
that application is getting crash.Is there any efficient was to create csv record.
2) Also the csv file is displaying data properly in excel but not in open office
eg:-
Open Office output
Account Number
19824:38:00
Excel output(Expected output)
Account Number
019465:021578
Can’t understand why OpenOffice is not showing proper output.
Thanks in Advance.
You definitely need to change your
dataToSendinstance variable to be of typeNSMuableStringand only append data to it. The way you do it now, creates a new string every time, but withNSMutableStringit will only append it.