我有数据导出到Excel它工作正常。 但我有一个小问题如何生成CSV文件?
我的输出导出这样的:
什么,我想发生是这样的:
,这是我的代码出口:
-(void)exportCSV {
NSArray * data = [NSArray arrayWithObjects:entries,keys, nil];
NSLog(@"%@",data);
csv =[NSMutableString string];
for (NSArray * line in data) {
NSMutableArray * formattedLine = [NSMutableArray array];
for (field in line) {
BOOL shouldQuote = NO;
NSRange r = [field rangeOfString:@","];
//fields that contain a , must be quoted
if (r.location != NSNotFound) {
shouldQuote = YES;
}
r = [field rangeOfString:@"\""];
//fields that contain a " must have them escaped to "" and be quoted
if (r.location != NSNotFound) {
field = [field stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
shouldQuote = YES;
}
if (shouldQuote == YES) {
[formattedLine addObject:[NSString stringWithFormat:@"\"%@\"\"%@\"", entries,keys]];
} else {
[formattedLine addObject:field];
}
}
NSString * combinedLine = [formattedLine componentsJoinedByString:@";"];
[csv appendFormat:@"%@\n", combinedLine];
NSLog(@"%@",csv);
}
}
它看起来像'data'中的第一行是列标题,后续行包含数据。既然你一次只处理一行内容,你就能得到你所期望的。要获得转置,您需要将所有数据读入数组的列中,然后将其读出(反之亦然)。或者,第一次打开Excel文件后,请执行“选择全切 - 粘贴特殊转置”。后者会更容易... – Floris
这个问题与Excel无关。这是一个“生成CSV”的问题。 – rmaddy