我正在使用以下代码创建 .csv 文件
-(NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"myfile.csv"];
}
-(无效)创建文件{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
[[NSFileManager defaultManager] createFileAtPath: [self dataFilePath] contents:nil attributes:nil];
}
for (int i=0; i<[arr count]; i++) {
NSString *_amnt = [[self.reversedArr objectAtIndex:i] valueForKey:@"amount"];
writeString = [NSString stringWithFormat:@"%@,%@,%@", [[arr objectAtIndex:i] valueForKey:@"date"], [[arr objectAtIndex:i] valueForKey:@"particular", [NSString stringWithFormat:@"%@\n", _amnt]];
}
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]];
}
我成功创建了 csv 文件,但是当我在 excel 表中打开 .csv 文件时遇到问题,然后是任何有更多字符的行,根据字符扩展其列宽,但我需要用固定列宽换行文本,行高应该延长。那么我应该为这个任务做些什么。任何人都可以提出建议。