如何将核心数据导出到 CSV [英] how to export Core Data to 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.
谁能教我如何使用 CHCSVParser
写入 CSV 文件?
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);
}
推荐答案
CHCSVWriter
有几种构造 CSV 文件的方法:
A CHCSVWriter
has several methods for constructing CSV files:
-writeField:
接受一个对象并将其 -description(在正确转义后)写入 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:
接受一个以逗号分隔且以 nil 结尾的对象列表,并将每个对象发送到 -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:
接受以逗号分隔且以 nil 结尾的对象列表,将每个对象发送到 -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
.
这样的东西应该适合你.
Something Like this should work for you.
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);
这篇关于如何将核心数据导出到 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!