我在我的应用程序中附加或创建.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];
答案 0 :(得分: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
答案 1 :(得分: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];