如何将excel中数据转换成plist文件

excel表格中数据格式如图所示:

如何将excel中数据转换成plist文件_第1张图片

首先将excel表转换成csv文件,然后使用文本编辑器打开,如下图:

如何将excel中数据转换成plist文件_第2张图片

将内容复制到.txt文件中,然后在Xcode工程使用如下代码读取并写plist文件:

- (void)writeToPlist {    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shortcuts" ofType:@"txt"];
    NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",contents);

    NSArray *contentsArray = [contents componentsSeparatedByString:@"\r\n"];
    NSString *docs = [NSHomeDirectory() stringByAppendingPathComponent:@"shortcuts.plist"];
    NSLog(@"路径:%@", docs);
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSInteger idx;
    for (idx = 0; idx < contentsArray.count; idx++) {
        NSString* currentContent = [contentsArray objectAtIndex:idx];
        NSArray *timeDataArr = [currentContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setObject:[timeDataArr objectAtIndex:1] forKey:@"URL"];
        [dic setObject:[timeDataArr objectAtIndex:0] forKey:@"name"];
        [dic setObject:[timeDataArr objectAtIndex:2] forKey:@"type"];
        [arr addObject:dic];
      }
  [arr writeToFile:docs atomically:YES];
}

去模拟器沙盒中对应路径找到plist文件

你可能感兴趣的:(如何将excel中数据转换成plist文件)