如何写入数据并生成CSV,然后通过电子邮件作为附件发送?

时间:2012-12-19 04:08:12

标签: objective-c ios csv email-attachments

我有一张有6列的表。

我希望以附件形式写入CSV和电子邮件。

如何制作?

-(void)generateCSV
{
//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:[@"" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; //finally save the path (file)

surveyarray = [db retrieveSurvey];
CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:fullPath atomic:NO];
NSInteger numberOfColumns = 5;
for (NSInteger currentIndex = 0; currentIndex < [surveyarray count]; currentIndex++) {
    id field = [surveyarray objectAtIndex:currentIndex];
    [csvWriter writeField:field];
    if ((currentIndex % numberOfColumns) == (numberOfColumns - 1)) {
        [csvWriter writeLine];
    }
}
[csvWriter release];
}

这是我目前的职能。我在哪里可以找到我的csv文件或生成它?

1 个答案:

答案 0 :(得分:0)

您的一个问题似乎是我在哪里找到我写的文件:在回答这个问题时,如果要保存文件,则会将文件保存到应用的文档目录中。您可以通过启用文件共享来检查这一点,以便您可以在iTunes中查看该目录。有关如何执行此操作的说明,请查看此处。

http://mobiledevelopertips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html