iOS将excel转plist

时间:2022-01-18 14:14:49

iOS将excel转plist

先把excel用Numbers打开,转换成CSV,然后新建一个工程,写下面的代码:

- (void)viewDidLoad {
[super viewDidLoad];
[self writeToPlist];
} - (void)writeToPlist {
NSString *path = [[NSBundle mainBundle] pathForResource:@"高德地图API 城市编码表 2" ofType:@"csv"];
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 = ; idx < contentsArray.count; idx++) {
NSString* currentContent = [contentsArray objectAtIndex:idx];
NSArray *timeDataArr = [currentContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:[timeDataArr objectAtIndex:] forKey:@"postCode"];
[dic setObject:[timeDataArr objectAtIndex:] forKey:@"locationName"];
[dic setObject:[timeDataArr objectAtIndex:] forKey:@"phoneCode"];
[arr addObject:dic];
}
[arr writeToFile:docs atomically:YES];
}