无法生成csv文件

时间:2012-12-19 06:52:47

标签: objective-c ios csv

-(void)generateCSV
{
NSFileManager *filemgr = [NSFileManager defaultManager];

[filemgr createFileAtPath:@"test.csv" contents:nil attributes: nil];
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:@"test.csv"];

NSLog (@"why null %@", file);
[file seekToEndOfFile];

NSLog(@"%@", file);

surveyarray = [db retrieveSurvey];

for(int i = -1; i < surveyarray.count; i++)
{
    if(i == -1)
    {
        NSString *str = [NSString stringWithFormat: @"No, CreatedDate, Question1, Question2, Question3, Comment\n"];
        [file writeData: [str dataUsingEncoding: NSUTF16LittleEndianStringEncoding]];
    }
    else
    {
        survey = [surveyarray objectAtIndex:i];
        NSString *str = [NSString stringWithFormat: @"%d, %@, %@, %@, %@, %@\n", i,  survey.createddate, survey.question1, survey.question2, survey.question3, survey.comment];
        [file writeData: [str dataUsingEncoding: NSUTF16LittleEndianStringEncoding]];
    }
}

[file closeFile];

/*CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:fullPath atomic:NO];

NSInteger numberOfColumns = 5;

for (NSInteger currentIndex = 0; currentIndex < [surveyarray count]; currentIndex++)
{
    id field = [surveyarray objectAtIndex:currentIndex];

    [csvWriter writeField:field];

    if ((currentIndex % numberOfColumns) == (numberOfColumns - 1))
    {
        [csvWriter writeLine];
    }
}

[csvWriter release];*/
}

我已记录该文件并返回(null)

日志输出

2012-12-19 14:46:55.853 OCBC[6725:907] (null)
2012-12-19 14:46:55.860 OCBC[6725:907] 2012-12-19 14:14:31
2012-12-19 14:46:55.862 OCBC[6725:907] Interview
2012-12-19 14:46:55.863 OCBC[6725:907] Visitor
2012-12-19 14:46:55.865 OCBC[6725:907] Meet Expectation
2012-12-19 14:46:55.866 OCBC[6725:907] N/A

1 个答案:

答案 0 :(得分:1)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath = [documentsDir stringByAppendingPathComponent:@”test.csv”];

尝试将filePath传递给您的方法