在iOS中从数据数组创建csv文件 [英] Create csv file from array of data in iOS

查看:104
本文介绍了在iOS中从数据数组创建csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从sql文件写入csv文件。我收集了所有的数据从sql文件在一个数组和使用for循环我是附加和写入数据到.CSV文件。但似乎它显示在一行中的数据,只有它不去新行创建新行。
我已使用作为参考。
这是我的代码:

I want to write data from sql file to csv file. I have collected all data from sql file in an array and using for loop i am appending and writing data to .csv file. but it seems that it shows data in one line only it does not go to new line to create new row. I have used this for reference. This is my code :

-(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]];      
    }    
}


推荐答案

只写一行,因为每次你通过你的循环重写文件。最好不要在文件上 writeData ,直到循环完成。我还将使用像这样的NSMutableString:

This only writes one line because you rewrite the file every time you go through your loop. It is best to not writeData on the file until the loop has completed. I would also use an NSMutableString like this:

- (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]];
}    

这篇关于在iOS中从数据数组创建csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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