将 excel sheet 列解析为 iOS 中的 NSArray
Parsing excel sheet columns into NSArray in iOS
我通过转换 excel 文档制作了几个 csv 文件。下面的代码用于读取 csv 文件。我尝试按索引解析,但它返回的是行值而不是列
NSString *sourceFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GanditProState" ofType:@"csv"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *csvArray = [[NSMutableArray alloc] init];
csvArray = [[sourceFileString componentsSeparatedByString:@"\n"] mutableCopy];
dict = [[NSMutableDictionary alloc] init];
NSLog(@"dict %@",dict);
谁能给我解析列的解决方案。
您可以删除行:
NSMutableArray *csvArray = [[NSMutableArray alloc] init];
并将其更改为行:
NSArray* csvArray = [[sourceFileString componentsSeparatedByString:@"\n"] mutableCopy];
行的每个元素 divided 由“,”组成。要解析每一行,你可以使用这个:
[csvArray enumerateObjectsUsingBlock:^(NSString* lineString, NSUInteger idx, BOOL *stop) {
NSArray* lineComponents = [lineString componentsSeparatedByString:@","];
//TODO: do smth this lines components;
}];
但我认为,更好的方法是使用一些库来解析 csv,example。
P.S.
componentsSeparatedByString: - returns NSArray.
假设第一行包含列名。
// Split the file into lines
NSMutableArray *lines = [source componentsSeparatedByString:@"\n"].mutableCopy;
// Get the first line as column headings
NSArray *keys = [lines.firstObject componentsSeparatedByString:@","];
// Initialize the result object, an array of dictionaries with
// headings as keys and arrays as values for each key
NSMutableArray *result = [NSMutableArray array];
for (int i = 0; i<keys.count; i++) {
[result addObject:@{keys[i] : [NSMutableArray array]}];
}
// Remove the first line which is already stored in keys array
[lines removeObjectAtIndex:0];
for (NSString *line in lines) {
// Get a list of all values
NSArray *columns = [line componentsSeparatedByString:@","];
for (int i = 0; i<keys.count; i++) {
// Insert the value into the array of the proper key
NSMutableArray *values = result[i][keys[i]];
[values addObject:columns[i]];
}
}
我认为这比仅将列标题作为键的字典要好,因为它保留了列的顺序。但是如果你不需要,你可以简化为只有一个包含键和数组的字典。
我通过转换 excel 文档制作了几个 csv 文件。下面的代码用于读取 csv 文件。我尝试按索引解析,但它返回的是行值而不是列
NSString *sourceFileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"GanditProState" ofType:@"csv"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *csvArray = [[NSMutableArray alloc] init];
csvArray = [[sourceFileString componentsSeparatedByString:@"\n"] mutableCopy];
dict = [[NSMutableDictionary alloc] init];
NSLog(@"dict %@",dict);
谁能给我解析列的解决方案。
您可以删除行:
NSMutableArray *csvArray = [[NSMutableArray alloc] init];
并将其更改为行:
NSArray* csvArray = [[sourceFileString componentsSeparatedByString:@"\n"] mutableCopy];
行的每个元素 divided 由“,”组成。要解析每一行,你可以使用这个:
[csvArray enumerateObjectsUsingBlock:^(NSString* lineString, NSUInteger idx, BOOL *stop) {
NSArray* lineComponents = [lineString componentsSeparatedByString:@","];
//TODO: do smth this lines components;
}];
但我认为,更好的方法是使用一些库来解析 csv,example。
P.S.
componentsSeparatedByString: - returns NSArray.
假设第一行包含列名。
// Split the file into lines
NSMutableArray *lines = [source componentsSeparatedByString:@"\n"].mutableCopy;
// Get the first line as column headings
NSArray *keys = [lines.firstObject componentsSeparatedByString:@","];
// Initialize the result object, an array of dictionaries with
// headings as keys and arrays as values for each key
NSMutableArray *result = [NSMutableArray array];
for (int i = 0; i<keys.count; i++) {
[result addObject:@{keys[i] : [NSMutableArray array]}];
}
// Remove the first line which is already stored in keys array
[lines removeObjectAtIndex:0];
for (NSString *line in lines) {
// Get a list of all values
NSArray *columns = [line componentsSeparatedByString:@","];
for (int i = 0; i<keys.count; i++) {
// Insert the value into the array of the proper key
NSMutableArray *values = result[i][keys[i]];
[values addObject:columns[i]];
}
}
我认为这比仅将列标题作为键的字典要好,因为它保留了列的顺序。但是如果你不需要,你可以简化为只有一个包含键和数组的字典。