无法附加或创建.csv文件(使用CHCSParser)



我在应用程序中附加或创建.csv文件时遇到问题,我无法确定问题所在。在电子邮件视图中,会显示.csv附件,但当收到电子邮件时,它没有附件。我将一个对象数组(dataController.masterList)发送到CHCSVFriter。本周我花了很多时间尝试其他关于电子邮件附件和CHCSVFriter的问题的解决方案,显然没有一个解决方案奏效,所以我知道我在问你。问题出在哪里?你有什么建议?提前感谢您,快乐的日子,-罗伯

- (IBAction)send:(id)sender {
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
}
NSString *filepath = @"testfile.csv";
filepath = [filepath stringByExpandingTildeInPath];
NSOutputStream *exportStream = [NSOutputStream outputStreamToFileAtPath:filepath append:NO];
NSStringEncoding encodingA = NSUTF8StringEncoding;
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithOutputStream:exportStream encoding:encodingA delimiter:','];
[csvWriter writeField:[NSString stringWithFormat:@"One"]];
[csvWriter writeLineOfFields:dataController.masterList];
[csvWriter closeStream];
NSString *path = [[NSBundle mainBundle] pathForResource:filepath ofType:@".csv"];
NSData *mydata = [NSData dataWithContentsOfFile:path];
if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    [mail setMailComposeDelegate:self];
    [mail setSubject:@"CSV File"];
    [mail addAttachmentData:mydata mimeType:@"text/csv" fileName:filepath];
    [mail setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:mail animated:YES completion:nil];
    }
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];

我想明白了,我停止使用CHCSV编写器,只写了一个数组,然后用","组合数组的组件。

- (IBAction)send:(id)sender {
static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
}
NSIndexPath *index2 = 0;
NSUInteger i = 0;
NSString *holder;
NSArray *holderArray;
NSArray *saverArray;
while (i < dataController.countOfList) {
TimeSheetEntry *sAtIndex = [self.dataController objectInListAtIndex:index2.row];
    NSString *dayhold = [formatter stringFromDate:sAtIndex.date];
    holderArray = [[NSArray alloc] initWithObjects:sAtIndex.name, sAtIndex.jobnum, sAtIndex.hours, sAtIndex.jobnotes, dayhold, nil];
    saverArray = [saverArray arrayByAddingObjectsFromArray:holderArray];
    i++;
    NSIndexPath *index3 = [NSIndexPath indexPathForRow:i inSection:1];
    index2 = index3;
}
holder = [saverArray componentsJoinedByString:@","];//this is the seperating variable
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *outputFile = [docDirectory stringByAppendingPathComponent:@"timesheet.csv"];
NSError *csvError = NULL;
BOOL written = [holder writeToFile:outputFile atomically:YES encoding:NSUTF8StringEncoding error:&csvError];
if (!written)
    NSLog(@"write failed, error=%@", csvError);

if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    [mail setMailComposeDelegate:self];
    [mail setSubject:@"CSV File"];
    //[mail setMessageBody:holder isHTML:YES];
    [mail addAttachmentData:[NSData dataWithContentsOfFile:outputFile] mimeType:@"text/csv" fileName:@"timesheet.csv"];
    [mail setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:mail animated:YES completion:nil];
    }
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
}

@end

对于仍然想使用CHCSParser的人,我发现文件路径必须是绝对的:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
                                          inManagedObjectContext:_tempManagedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [_tempManagedObjectContext executeFetchRequest:fetchRequest error:&error];
NSURL *datapath = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"export.csv"];
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];
// Fetch objects to write to .csv
for (Task *task in fetchedObjects) {
    [writer writeLineOfFields:@[task.taskID, task.taskTitle, task.taskDescription]];
}
entity = [NSEntityDescription entityForName:@"Journal"
                                          inManagedObjectContext:_tempManagedObjectContext];
[fetchRequest setEntity:entity];
fetchedObjects = [_tempManagedObjectContext executeFetchRequest:fetchRequest error:&error];
for (JournalEntry *entry in fetchedObjects) {
    [writer writeLineOfFields:@[entry.day, entry.entryTitle, entry.entryDescription]];
}
[writer closeStream];
NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
[buffer writeToURL:datapath atomically:NO];

相关内容

  • 没有找到相关文章

最新更新