如何将Core Data导出为CSV [英] how to export Core Data to CSV

查看:285
本文介绍了如何将Core Data导出为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 CHCSVParser 将Core数据导出为CSV。我知道如何从实体获得所有的价值,但我不知道如何写入CSV。

I will like to use CHCSVParser to export my Core data to CSV. I know how to get all the value from entity, but I don't know how to write to CSV.

任何人都可以教我如何写CSV到 CHCSVParser

Can anybody teach me how to write to CSV with CHCSVParser?

// Test listing all Infos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"NoteLog" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NoteLog *noteInfo in fetchedObjects) {

    NSLog(@"Name: %@", noteInfo.city );
    NSLog(@"Name: %@", noteInfo.country);
    NSLog(@"Name: %@", noteInfo.datetime);
    NSLog(@"Name: %@", noteInfo.notelatitude);
    NSLog(@"Name: %@", noteInfo.notelongtitude);
    NSLog(@"Name: %@", noteInfo.state);
    NSLog(@"Name: %@", noteInfo.text);        
}  


推荐答案

A CHCSVWriter 有几种构建CSV文件的方法:

A CHCSVWriter has several methods for constructing CSV files:

-writeField:对象并将其描述(正确转义后)写入CSV文件。如果需要,它还将写入字段分隔符(,)。您可以传递一个空字符串(@)或nil来写一个空字段。

-writeField: accepts an object and writes its -description (after being properly escaped) out to the CSV file. It will also write field seperator (,) if necessary. You may pass an empty string (@"") or nil to write an empty field.

-writeFields:接受逗号分隔和无终止的对象列表,并将每个对象发送到-writeField:

-writeFields: accepts a comma-delimited and nil-terminated list of objects and sends each one to -writeField:.

-writeLine 用于终止当前CSV行。如果您不调用-writeLine,则所有CSV字段将在一行中。

-writeLine is used to terminate the current CSV line. If you do not invoke -writeLine, then all of your CSV fields will be on a single line.

-writeLineOfFields:接受以逗号分隔和以零结束的对象列表,将每个对象发送到-writeField :,然后调用-writeLine。

-writeLineOfFields: accepts a comma-delimited and nil-terminated list of objects, sends each one to -writeField:, and then invokes -writeLine.

-writeLineWithFields:接受对象数组,将每个对象发送到-writeField :,然后调用-writeLine。

-writeLineWithFields: accepts an array of objects, sends each one to -writeField:, and then invokes -writeLine.

-writeCommentLine:接受一个字符串并将其作为CSV风格的注释写入文件。

-writeCommentLine: accepts a string and writes it out to the file as a CSV-style comment.

除了写入文件, CHCSVWriter 可以初始化为直接写入 NSString

In addition to writing to a file, CHCSVWriter can be initialized for writing directly to an NSString.

这样应该对你有用。

CHCSVWriter *writer = [[CHCSVWriter alloc] initForWritingToString];

for (NoteLog *noteInfo in fetchedObjects) {

    [writer writeLineOfFields:noteInfo.city, noteInfo.country, noteInfo.datetime, noteInfo.notelatitude, noteInfo.notelongtitude, noteInfo.state, noteInfo.text, nil];     
}  

NSLog(@"My CSV File: %@",writer.stringValue);

这篇关于如何将Core Data导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆