我已经将数据导出到Excel中,它运行得很好。
但是我有一个小问题:
我的输出像这样导出:
我希望发生的情况是这样的:
这是我的导出代码:
-(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