2014-02-21 71 views
1

在我的應用程序中,我創建了一個.plist文件,但我需要使用存儲在此.plist中的數據存儲在Excel表中。我需要一種方法來獲得這個plist的csv。我試圖在網上搜索,但我發現只有轉換器允許從csv創建一個plist。我想有一種方法可以直接從我的iPad應用程序生成csv文件。有誰知道一種方法來做到這一點?從NSMutableArray創建一個csv

UPDATE:

+ (void)saveFileFromArray:(NSMutableArray *)promoArray { 
    NSMutableString *target = [@"Nome,Cognome,E-mail,Data di nascita,Indirizzo,Città,Cap,Telefono\n" mutableCopy]; 

    for (NSDictionary *dict in promoArray) { 
     [target appendFormat:@"%@,%@,%@,%@,%@,%@,%@,%@\n",dict[@"name"],dict[@"surname"],dict[@"email"],dict[@"birthdate"],dict[@"address"],dict[@"city"],dict[@"zip"],dict[@"phone"]]; 
    } 
    NSError *error = nil; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"profiles.csv"]; 

    NSURL *url = [NSURL URLWithString:fileName]; 

    BOOL success = [target writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

    if (!success) { 
     NSLog(@"Errore: %@", error.localizedDescription); 
    } 
} 
+0

在應用程序中將plist轉換爲CSV,然後導出文件,以便在Excel中訪問... http://stackoverflow.com/questions/10119035/create-csv-file-from-array-of- data-in-ios – Tim

回答

1

迭代通過數組,並寫出你想,一個字符串的部分。

帶有逗號的字符串將有相當大的幫助。馬車回報也很好。

當字符串完成。將它寫入文件。

NSMutableString *target = [@"head1,head2,head3\n" mutableCopy]; 

for (NSDictionary *dict in array) { 

    [target appendFormat:@"%@,%@,%@\n",dict[@"thing"],dict[@"otherthing"],dict[@"lemming"]]; 

} 

NSError *error = NULL; 

BOOL success = [target writeToURL:somewhere atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

if (!success) { 
    NSLog(@"oh no! - %@",error.localizedDescription); 
} 

該段假定你有NSDictionary陣列但如果它的別的東西的原則是一樣的。

對於每個對象都打印出逗號分隔的字符串並以CR結束。

事情你將不得不在現實世界的數據,以防止有

  • 描述與已經是逗號。
  • 描述與CR已在。
+0

謝謝!我剛剛改變了一個指令:'BOOL success = [target writeToURL:somewhere atomically:YES encoding:NSUTF8StringEncoding error:&error];'to'BOOL success = [target writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];'and現在效果很好! – lucgian841

0

只是使用已經開發和測試的框架。我使用https://github.com/davedelong/CHCSVParser,它工作正常。

偶爾,我不得不添加一個或兩個特性,但鑑於它是開源的,它並不是什麼大不了的事情。