Sometimes we need to import a lot of data into the app static data, but the data is in Excel, what to do?
First, copy the data into a. txt text document
Second, it's the code.
For example, I need to import a data table with 2 columns of fields, name and barcode
1 //Path2NSString *path = [[NSBundle mainbundle] Pathforresource:@"Tiaoxingma"OfType:@"txt"];3 //UTF-84NSString *contents =[[NSString alloc] Initwithcontentsoffile:path encoding:nsutf8stringencoding Error:nil];5 //string Splitting6Nsarray *contentsarray =[contents Componentsseparatedbycharactersinset:[nscharacterset Newlinecharacterset];7 //Storage location8NSString *docs = [Nshomedirectory () stringByAppendingPathComponent:@"documents/tiaoxingma.plist"] ;9 //Defining ArraysTenNsmutablearray *arr =[Nsmutablearray array]; One A Nsinteger idx; - for(idx =0; IDX < Contentsarray.count; idx++) { -nsstring* currentcontent =[Contentsarray Objectatindex:idx]; the //String Split tab -nsarray* Timedataarr = [Currentcontent componentsseparatedbycharactersinset:[nscharacterset Charactersetwithcharactersinstring:@"\ t"]]; - -Nsmutabledictionary *dic =[Nsmutabledictionary dictionary]; +[DiC Setobject:[timedataarr Objectatindex:0] Forkey:@"name"]; -[DiC Setobject:[timedataarr Objectatindex:1] Forkey:@"BarCode"]; + [arr addobject:dic]; A } at - //data written to plist file -[arr Writetofile:docs Atomically:yes];
Convert Excel document data to plist file