how to export Core Data to CSV
Asked Answered
L

2

6

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.

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);        
}  
Linstock answered 13/5, 2012 at 13:33 Comment(0)
A
8

A CHCSVWriter has several methods for constructing CSV files:

-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: accepts a comma-delimited and nil-terminated list of objects and sends each one to -writeField:.

-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: accepts a comma-delimited and nil-terminated list of objects, sends each one to -writeField:, and then invokes -writeLine.

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

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

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);
Aubreir answered 13/5, 2012 at 13:59 Comment(5)
hi Rayan, Will you beable to give me an example ? all i need is the to export all the data out and thats itLinstock
Added some code for your particular case. If you paste that in you should get a nice CSV output in your console. From there I trust you know how to put it in a file and do whatever you want with it.Aubreir
Glad to hear :) Happy coding.Aubreir
hi i was trying your code but seems that "initForWritingToString" has been replaced in CHCSVParser. can u help me out. I have the same issueIphigenia
It no longer writes to a string. Now it only writes directly to a path. initForWritingToCSVFile:(NSString *)path So just give it a path where you'd like your CSV file saved. You could store it somewhere temporary and read it to a string using stringWithContentsOfFile: if you really need it in a string.Aubreir
M
8

The answer above seems to be deprecated, the author replaced the method with another it seem. This worked for me, hope it helps:

NSOutputStream *stream = [[NSOutputStream alloc] initToMemory];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:stream encoding:NSUTF8StringEncoding delimiter:','];

for (Type *instance in fetchedResults) {
    [writer writeLineOfFields:@[instance.propertyA, instance.B]];
}
[writer closeStream];

NSData *buffer = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
NSString *output = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
Mydriasis answered 26/1, 2014 at 22:15 Comment(3)
what is fetchedResults?Goodly
@LukeIrvin, an array of database entities. See the original post - NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];Hastate
Any idea how to use writeLineOfFields with swift?Eckart

© 2022 - 2024 — McMap. All rights reserved.