Excel转Plist

将excel文件转化为plist文档 

1、把 excel 文件转换成.csv文件,mac下直接使用numbers软件就可以实现,文件-->导出到 csv。
操作时保留excel 文件中的第一行的列名。
2、具体的代码:
NSString *iOSPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"csv"];
    NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *baseStationInfoArr= [content componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSString *names = [baseStationInfoArr objectAtIndex:0];
    NSMutableArray *baseStations = [baseStationInfoArr mutableCopy];
    [baseStations removeObjectAtIndex:0];
    NSMutableArray *allPlists = [NSMutableArray array];
    //    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"baseStation" ofType:@"plist"];
    NSString *plistPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test2.plist"];
   
    for (int i = 0; i < baseStations.count; i++) {
        
        if (i % 2 == 0) {
            continue;
        }
        NSArray *allTitle = [names componentsSeparatedByString:@","];
        NSArray *allContents = [baseStations[i] componentsSeparatedByString:@","];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (int j = 0; j < allContents.count; j++) {
            [dic setObject:allContents[j] forKey:allTitle[j]];
        }
        [allPlists addObject:dic];
    }
    [allPlists writeToFile:plistPath atomically:YES];



你可能感兴趣的:(iOS)