0

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

- (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];
4

2 回答 2

0

我想通了,我停止使用 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
于 2013-08-14T18:20:02.157 回答
0

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

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];
于 2014-10-07T15:24:40.637 回答