I want to create a plist file from an excel file. The structure of excel and plist is defined.
Excel file has only two columns, one is “key” and the other is corresponding “value” column.
So the final plist that will be generated is of the form:
<key>keyString1</key> <string>valueString1</string> <key>keyString2</key> <string>valueString2</string> <key>keyString3</key> <string>valueString3</string> ............................. ............................. ............................. <key>keyStringn</key> <string>valueStringn</string>
Now the problem is that the string values for “keys” and “values” are inturn strings i.e. they are not one word string. They may go more than one line.Hence I can’t use csv file structure here.
Need your urgent help.
Please suggest how to read values from excel sheet so that both column values can be separated.Any sample code will add aid.
I accomplished this by writing a formula in excel to create the text of the plist file for you. Its pretty easy.
1) (optional) In XCode, create a plist that has the formatting you want in the end. Save, and go to this in finder. Open it in textWrangler or text edit so that you can see what the tags are for each element in your plist.
For example, you should find something like:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>First Key Name</key> <array> <string>Before God we are all equally wise</string> <string></string> <string>Albert Einstein</string> <integer>0</integer> </array> <key>Second Key Name</key> <array> <string>Do not worry about your troubles.</string> <string></string> <string>Albert Einstein</string> <integer>0</integer> </array> </dict> </plist>
2) I will assume:
– Your spreadsheet represents a description of the same 4 properties for many rows/Objects/Levels
– You have each array as a separate row in your spreadsheet
– The same properties are being described for every row in the same order.
– You will be able to access the items in the array by knowing which index they are stored at. ie, I would get the second Author by looking in the second array at index 2.
Your spreadsheet should look something like this:
A B C D E F /--(Key Names)--\/-------------------(Properties of Each)-----------------\ 1 Level Name | Quote | Notes | Author | Favorited | ____________________________________________________________________________ 2 First Key Name | Before God... | | Albert Einstein | 0 | 3 Second Key Name | Do not wor... | | Albert Einstein | 0 | . . .
So each of my objects will get its own key in my dictionary and each object is described by 3 Strings and an integer.
3) The Formula
On the right of the last column in my spreadsheet, I will write a formula that generates the xml code for that row:
(F2) = CONCATENATE ("<key>",A2,"</key> <array> <string>",B2,"</string> <string>",C2,"</string> <string>",D2,"</string> <integer>",E2,"</integer> </array>")
Drag the lower-right corner of this cell down into all the other rows and they will automatically populate the information for each row.
Similarly, at the bottom of the spreadsheet, under all the rows you can use another formula to concatenate the rows. Say you had 300 rows:
(F4) = CONCATENATE (F2:F300)
Now copy this data into an text file, and add the last few tags and doctype information, which you can copy from above. save with the extension .plist and you’re good to go! Add the file back into your xcode project and load it like any other plist.
To export data from Excel to a text file, use the Save As command.
There are two commonly used text file formats:
Delimited text files (.txt), in which the TAB character (ASCII character code 009) typically separates each field of text.
Comma separated values text files (.csv), in which the comma character (,) typically separates each field of text.
You can change the separator character that is used.