Excel文档数据转成Plist文件

  有时候我们需要导入大量数据到App中静态数据,但数据又是存在Excel中,怎么办?

  第一,复制数据粘贴到一个.txt文本文档中

  第二,就是撸代码了

  比如,我需要导入的数据表有2列字段,name和barCode

 1 // path
 2     NSString *path = [[NSBundle mainBundle] pathForResource:@"tiaoxingma" ofType:@"txt"];
 3     // UTF-8
 4     NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
 5     // 字符串拆分
 6     NSArray *contentsArray = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
 7     // 存储位置
 8     NSString *docs = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/tiaoxingma.plist"] ;
 9     // 定义数组
10     NSMutableArray *arr = [NSMutableArray array];
11     
12     NSInteger idx;
13     for (idx = 0; idx < contentsArray.count; idx++) {
14         NSString* currentContent = [contentsArray objectAtIndex:idx];
15         // 字符串拆分 制表符
16         NSArray* timeDataArr = [currentContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\t"]];
17         
18         NSMutableDictionary *dic = [NSMutableDictionary dictionary];
19         [dic setObject:[timeDataArr objectAtIndex:0] forKey:@"name"];
20         [dic setObject:[timeDataArr objectAtIndex:1] forKey:@"barCode"];
21         [arr addObject:dic];
22     }
23     
24     // 数据写入plist文件
25     [arr writeToFile:docs atomically:YES];

 

你可能感兴趣的:(Excel文档数据转成Plist文件)