core-data - 使用 CHCSVParser 通过邮件编辑器将核心数据导出到 csv(作者:Dave DeLong)

标签 core-data csv

我正在使用 CHCSVParser(由 Dave DeLong 编写)通过邮件编辑器将核心数据导出到 csv 中,但问题是它采用的是核心数据表名称而不是行值。

另外,我希望它们按特定顺序排列。例如“下水道编号”、“人孔 1”、“人孔 2”

这是我正在使用的编码:

//从核心数据中获取数据

NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Input_Details" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];


request.predicate = [NSPredicate predicateWithFormat:@"rs_Input_project.name = %@", self.projectObject.name];


[request setEntity:entityDescription];
request.resultType = NSDictionaryResultType;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sewer_No" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error;

NSArray *fetchedObjects = [moc executeFetchRequest:request error:&error];



 //creating a csv CHCSVWriter
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];

for (id object in fetchedObjects)
{
    if ([object conformsToProtocol:@protocol(NSFastEnumeration)])
    {
        [writer writeLineOfFields:object];

    }
}

[writer closeStream];


NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];

NSString *string = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];

NSLog(@"Length of Buffer:%d Error:%@",[buffer length],[error localizedDescription]);

if ( [MFMailComposeViewController canSendMail] )
{


    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;

    NSData *myData = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"myData csv:%@",myData);
     NSLog(@"string csv:%@",string);

// Fill out the email body text
    NSString *emailBody = @"Sewer_Output";
    [mailComposer setMessageBody:emailBody isHTML:NO];

    //attaching the data and naming it Sewer_Output
    [mailComposer addAttachmentData:myData  mimeType:@"text/cvs" fileName:@"Sewer_Output.csv"];

[self presentModalViewController:mailComposer animated:YES];
}

最佳答案

//creating a csv CHCSVWriter
NSOutputStream *output = [NSOutputStream outputStreamToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];
//wrting header name for csv file
[writer writeField:@"Line No"];
[writer writeField:@"Sewer No"];
[writer writeField:@"Manhole Up"];
[writer writeField:@"Manhole Down"];

[writer finishLine];



for (NSManagedObject *object in fetchedObjects)
{
    //getting the data from core data


    int line_no = [[object valueForKey:@"line_no"] intValue];
    int sewer_No = [[object valueForKey:@"sewer_No"] intValue];
    NSString *mh_Up=[object valueForKey:@"mh_Up"];
    NSString *mh_down=[object valueForKey:@"mh_down"];

        //writing that data to writer for csv file

        [writer writeField:[NSString stringWithFormat:@"%i",line_no]];
        [writer writeField:[NSString stringWithFormat:@"%i",sewer_No]];
        [writer writeField:[NSString stringWithFormat:@"%@",mh_Up]];
        [writer writeField:[NSString stringWithFormat:@"%@",mh_down]];

        [writer finishLine]; //finishing the writing of first row

}

[writer closeStream];


NSData *buffer = [output propertyForKey:NSStreamDataWrittenToMemoryStreamKey];

NSString *string = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
//NSLog(@"string = %@",string);

谢谢

关于core-data - 使用 CHCSVParser 通过邮件编辑器将核心数据导出到 csv(作者:Dave DeLong),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14693741/

相关文章:

iphone - iOS CoreData 更新实体对象

ios - 如何从 modalVC 中删除 VC 1 中的 tableView 行?

PHP 导入带有 utf-8 重音符号的 CSV

java - 在 Reducer 中获取输入文件

java - 读取 CSV 文件时出现的问题让我困惑不已

objective-c - 点语法和 valueForKey 之间的区别

ios - 删除 coredata 中保存的对象后,该对象仍保留在之前的 tableViewController 中

sql - 如何记录 CoreData SQL 查询?

php - 强制下载 CSV 文件

sql - 计算包含 25 年数据的 Access 表中每月的记录数