2012-04-12 5 views
14

में डेटा की सरणी से सीएसवी फ़ाइल बनाएं मैं एसक्यूएल फ़ाइल से सीएसवी फ़ाइल में डेटा लिखना चाहता हूं। मैंने एसक्यूएल फ़ाइल से सभी डेटा एक सरणी में एकत्र किया है और लूप के लिए उपयोग कर रहा हूं, मैं .csv फ़ाइल में डेटा जोड़ रहा हूं और लिख रहा हूं। लेकिन ऐसा लगता है कि यह एक पंक्ति में डेटा दिखाता है केवल नई पंक्ति बनाने के लिए यह नई लाइन पर नहीं जाता है। मैंने संदर्भ के लिए this का उपयोग किया है।आईओएस

-(NSString *)dataFilePath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return [documentsDirectory stringByAppendingPathComponent:@"myfile.csv"]; 
} 

- (IBAction)saveAsFileAction:(id)sender {  
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) { 
     [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil]; 
     NSLog(@"Route creato");   
    } 

    NSString *writeString;  
    for (int i=0; i<[dataArray count]; i++) {   
     writeString = [NSString stringWithFormat:@"%@, %@, %@, %@, %0.2f,",[[dataArray objectAtIndex:i]dates],[[dataArray objectAtIndex:i] time],[[dataArray objectAtIndex:i] category],[[dataArray objectAtIndex:i]place],[[dataArray objectAtIndex:i] amount]];   
     NSLog(@"writeString :%@",writeString);   
     NSFileHandle *handle; 
     handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ]; 
     //say to handle where's the file fo write 
     [handle truncateFileAtOffset:[handle seekToEndOfFile]]; 
     //position handle cursor to the end of file 
     [handle writeData:[writeString dataUsingEncoding:NSUTF8StringEncoding]];  
    }  
} 

उत्तर

35

यह केवल एक ही लाइन लिखते हैं क्योंकि आप फ़ाइल आप अपने पाश के माध्यम से जाना हर बार पुनर्लेखन: यह मेरा कोड है। लूप पूरा होने तक फ़ाइल पर writeData पर सबसे अच्छा नहीं है।

- (IBAction)saveAsFileAction:(id)sender { 

    if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) { 
     [[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil]; 
     NSLog(@"Route creato"); 
    } 

    NSMutableString *writeString = [NSMutableString stringWithCapacity:0]; //don't worry about the capacity, it will expand as necessary 

    for (int i=0; i<[dataArray count]; i++) { 
     writeString = [writeString appendString:[NSString stringWithFormat:@"%@, %@, %@, %@, %0.2f, \n",[[dataArray objectAtIndex:i]dates],[[dataArray objectAtIndex:i] time],[[dataArray objectAtIndex:i] category],[[dataArray objectAtIndex:i]place],[[dataArray objectAtIndex:i] amount]]]; //the \n will put a newline in 
     } 
    } 

    //Moved this stuff out of the loop so that you write the complete string once and only once. 
    NSLog(@"writeString :%@",writeString); 

    NSFileHandle *handle; 
    handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ]; 
    //say to handle where's the file fo write 
    [handle truncateFileAtOffset:[handle seekToEndOfFile]]; 
    //position handle cursor to the end of file 
    [handle writeData:[writeString dataUsingEncoding:NSUTF8StringEncoding]]; 
}  
+0

धन्यवाद। कोड पूरी तरह से ठीक काम कर रहा है .. धन्यवाद। –

+0

कोई समस्या नहीं, मदद करने में खुशी हुई। – sosborn

+0

अरे क्या आप जानते हैं कि सीएसवी फ़ाइल से उसी डेटा को वापस कैसे प्राप्त किया जाए? @Prernachavan – iSwaroop

5
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    NSString *root = [documentsDir stringByAppendingPathComponent:@"customers.csv"]; 

    NSString *temp; 

    temp = [NSString stringWithFormat:@"%@", [arrCustomersName objectAtIndex:0]]; 

    for (int i = 1; i < [arrCustomersName count]; i++) { 

     temp = [temp stringByAppendingFormat:@", %@", [arrCustomersName objectAtIndex:i]]; 
    } 

    [temp writeToFile:root atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 
0

बजाय:: मैं भी इस तरह की एक NSMutableString का प्रयोग करेंगे

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
NSString *root = [documentsDir stringByAppendingPathComponent:@"customers.csv"]; 

... बस लिखें: केवल एक छोटी लाइन के लिए

NSString *root = [NSHomeDirectory() stringByAppendingPathComponent:@"customers.csv"]; 

वास्तव में एक ही परिणाम कोड का ; ओ)

0
// For CSV File : 
     NSMutableString *stringToWrite = [[NSMutableString alloc] init]; 
[stringToWrite appendString:[NSString stringWithFormat:@"First Name,Last Name,Full Name,Phone Number, Email,Job, organizationName,Note\n\n"]]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     for(int i = 0 ;i<[Contact count];i++)  { 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"firstName"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"lastName"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact valueForKey:@"userName"] objectAtIndex:i]]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"phoneNumber"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"emailAddress"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"jobTitle"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@,",[[Contact objectAtIndex:i] valueForKey:@"organizationName"] ]]; 
      [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[Contact objectAtIndex:i] valueForKey:@"note"] ]]; 
     } 
     dispatch_async(dispatch_get_main_queue(), ^(void) { 
      NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
      NSString *documentDirectory=[paths objectAtIndex:0]; 
      NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.csv"]; 
      [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
     }); 
}); 
+0

मदद के लिए धन्यवाद, लेकिन कोड के पूरे ब्लॉक की तुलना में कुछ स्पष्टीकरण के साथ एक उत्तर बेहतर है – goto

0

इस यह मेरे लिए काम कर रहा है,

प्रयास करें किसी भी एक तेज 3

// MARK: CSV file creating 
    func creatCSV() -> Void { 

    let fileName = "Tasks.csv" 

    let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName) 
    var csvText = "Date,Task Name,Time Started,Time Ended\n" 

    for task in taskArr { 
     let newLine = "\(task.date),\(task.name),\(task.startTime),\(task.endTime)\n" 
     csvText.append(newLine) 
    } 

    do { 
     try csvText.write(to: path!, atomically: true, encoding: String.Encoding.utf8) 
    } catch { 
     print("Failed to create file") 
     print("\(error)") 
    } 
    print(path ?? "not found") 
    } 
    } 

में csv फ़ाइल बनाने के लिए अधिक जानकारी के लिए आप Detail Answer

उल्लेख कर सकते हैं चाहते हैं

उम्मीद है कि इससे किसी की मदद मिलेगी।