您不能将 sqlite 直接转换为 csv。首先,您获取所有数据库数据并写入 csv 文件。使用下面的示例创建 csv 并发送它。
NSString *csvString =@"1,2 , 3 , 4, 5 ,6 , 7";
for(int i=0;i<rangeData.count ;i++)
{
NSArray *temp = [di timeCalculation:[[rangeData objectAtIndex:i] objectForKey:@""] :[[rangeData objectAtIndex:i] objectForKey:@""]];
NSString *strTemp =[NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@",
[NSString stringWithFormat:@"%@",[simpleDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
[NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
[NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
[NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
[NSString stringWithFormat:@"%@",[shrotDate stringFromDate:[[NSDate alloc]initWithTimeIntervalSince1970:[[[rangeData objectAtIndex:i] objectForKey:@""] floatValue]]]],
[NSString stringWithFormat:@"%@",[[temp objectAtIndex:0] stringByReplacingOccurrencesOfString:@": " withString:@""]] ,
[NSString stringWithFormat:@"%@",[[temp objectAtIndex:1] stringByReplacingOccurrencesOfString:@" : " withString:@""]]];
csvString = [NSString stringWithFormat:@"%@ \n %@",csvString,strTemp];
}
//create instance of NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
//create an array and store result of our search for the documents directory in it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//create NSString object, that holds our exact path to the documents directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Document Dir: %@",documentsDirectory);
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.csv", @"userdata"]]; //add our file to the path
[fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSData *csvData =[NSData dataWithContentsOfFile:fullPath];
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObject:@""]];
[controller setSubject:@"CSV Export"];
[controller setMessageBody:@"" isHTML:NO];
[controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"userdata.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];