sam
sam

Reputation: 461

Parsing Excel file in iPhone

1)I want to parse Excel file, which i'll receive through safari in my application. How i'll do it?

2)How can i read excel file and output its output to command prompt programmatically?

3) How can i convert excel file to .csv file programmatically?

Upvotes: 1

Views: 3716

Answers (1)

olivaresF
olivaresF

Reputation: 1379

1) In order to parse the file from Safari you'd need to register first to be able to open XLS files. You have to add this information to your info.plist:

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>Microsoft Excel</string>
            <key>LSHandlerRank</key>
            <string>Alternate</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>org.openxmlformats.spreadsheetml.sheet</string>
                <string>com.microsoft.excel.xls</string>
            </array>
        </dict>
    </array>

Once your app is registered, the user will be able to see an "Open in ..." option while seeing the XLS file in Safari. This will launch either:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

or

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;

Depending on if your application is in memory or not.

2) I created QZXLSReader. It's a drag-and-drop solution so it's very easy to use. I don't think it's as feature complete, but it worked for me.

It's basically a library that can open XLS files and parse them into Obj-C classes. Once you have the classes, it's very easy to send them to Core Data or a dictionary or what have you.

3) Once you have opened the file with QZXLS reader, you can simply export it to a CSV file. Just iterating through all the rows and saving them to a txt file would be enough. From there, you can email, send in a message or do whatever you want with that txt file.

Upvotes: 2

Related Questions