Today, when testing, I encountered the need to convert a large amount of collated Excel data into a plist file. Baidu has a tutorial, found that although others have encountered similar situations, but some of the talk is not enough detail. So do the following. Baidu to the content of the use of mesasqlite, because there is no way, not to mention here temporarily. The actual process is as follows: 1. Convert the Excel file to a Notepad file. Copy and paste it out, this part of the operation I was under Windows, using Excel and notepad++. Using the default action also causes some problems later. 2. Directly drag the converted Notepad file into the Xcode resource folder, click Preview to the situation as follows, garbled: forced to run the program conversion, the program did not error, but the resulting plist file only the file header, the actual valid data is empty. The code is as follows:
NSString *alarmpath = [[NSBundle mainbundle] pathforresource:@ "gsk980m" oftype:@ "TXT"];//NSLog (@ "%@", Alarmpath); Attempt to change file encoding, failed//nsstringencoding GBK = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); NSString *alarmcontent = [[NSString alloc] Initwithcontentsoffile:alarmpath encoding:nsutf8stringencoding Error:nil] ;//NSLog (@ "%@", alarmcontent); Nsarray *alarmarray = [Alarmcontent componentsseparatedbycharactersinset:[nscharacterset newlineCharacterSet]];// NSLog (@ "%@", Alarmarray); NSString *docupath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; NSString *plistpath = [Docupath stringbyappendingpathcomponent:@ "Gsk980m.plist"]; NSLog (@ "%@", Plistpath); Nsmutablearray *resultsarr = [Nsmutablearray array]; for (Nsinteger j = 0; J < Alarmarray.count; J + +) {nsstring *alarmstr = [Alarmarray objectatindex:j];//NS Log (@ "%@", alarmstr); Nsarray *alarmarr = [Alarmstr componentsseparAtedbystring:@ ";"]; [Resultsarr addobject:@{@ "Alarmid": [Alarmarr objectatindex:0],@ "Alarmtype": [Alarmarr objectatindex:1],@ "Alarm": [ Alarmarr objectatindex:2],@ "ClearType": [Alarmarr objectatindex:3],@ "alarmcontent": [Alarmarr Objectatindex:4]}]; } [Resultsarr Writetofile:plistpath atomically:yes];
3. Search, confirm the issue of file encoding. Start by thinking about converting the encoding of the Notepad file, using the text editing that comes with your Mac. The app does not find the option to save as UTF-8. Continue Baidu, get the key information.Press the shortcut key in the text editor: Shift?+command?+t. When you save it, it will appear: then save it. 4. At this point, re-drag the file into the resource directory, the preview has been shown normal: Rerun the program to get the required plist file.
File encoding issues with Excel file to plist file